1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
package directories
import (
"fmt"
"net/url"
"strings"
"github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
)
// GetResourceID returns the Resource ID for the given Directory
// This can be useful when, for example, you're using this as a unique identifier
func (client Client) GetResourceID(accountName, shareName, directoryName string) string {
domain := endpoints.GetFileEndpoint(client.BaseURI, accountName)
return fmt.Sprintf("%s/%s/%s", domain, shareName, directoryName)
}
type ResourceID struct {
AccountName string
DirectoryName string
ShareName string
}
// ParseResourceID parses the Resource ID into an Object
// which can be used to interact with the Directory within the File Share
func ParseResourceID(id string) (*ResourceID, error) {
// example: https://foo.file.core.windows.net/Bar/Folder
if id == "" {
return nil, fmt.Errorf("`id` was empty")
}
uri, err := url.Parse(id)
if err != nil {
return nil, fmt.Errorf("Error parsing ID as a URL: %s", err)
}
accountName, err := endpoints.GetAccountNameFromEndpoint(uri.Host)
if err != nil {
return nil, fmt.Errorf("Error parsing Account Name: %s", err)
}
path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) == 0 {
return nil, fmt.Errorf("Expected the path to contain segments but got none")
}
shareName := segments[0]
directoryName := strings.TrimPrefix(path, shareName)
directoryName = strings.TrimPrefix(directoryName, "/")
return &ResourceID{
AccountName: *accountName,
ShareName: shareName,
DirectoryName: directoryName,
}, nil
}
|