Blame vendor/github.com/Azure/azure-storage-blob-go/azblob/url_container.go

Packit 63bb0d
package azblob
Packit 63bb0d
Packit 63bb0d
import (
Packit 63bb0d
	"bytes"
Packit 63bb0d
	"context"
Packit 63bb0d
	"errors"
Packit 63bb0d
	"fmt"
Packit 63bb0d
	"net/url"
Packit 63bb0d
Packit 63bb0d
	"github.com/Azure/azure-pipeline-go/pipeline"
Packit 63bb0d
)
Packit 63bb0d
Packit 63bb0d
// A ContainerURL represents a URL to the Azure Storage container allowing you to manipulate its blobs.
Packit 63bb0d
type ContainerURL struct {
Packit 63bb0d
	client containerClient
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// NewContainerURL creates a ContainerURL object using the specified URL and request policy pipeline.
Packit 63bb0d
func NewContainerURL(url url.URL, p pipeline.Pipeline) ContainerURL {
Packit 63bb0d
	client := newContainerClient(url, p)
Packit 63bb0d
	return ContainerURL{client: client}
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// URL returns the URL endpoint used by the ContainerURL object.
Packit 63bb0d
func (c ContainerURL) URL() url.URL {
Packit 63bb0d
	return c.client.URL()
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// String returns the URL as a string.
Packit 63bb0d
func (c ContainerURL) String() string {
Packit 63bb0d
	u := c.URL()
Packit 63bb0d
	return u.String()
Packit 63bb0d
}
Packit 63bb0d
Packit Service 3a6627
func (c ContainerURL) GetAccountInfo(ctx context.Context) (*ContainerGetAccountInfoResponse, error) {
Packit Service 3a6627
	return c.client.GetAccountInfo(ctx)
Packit Service 3a6627
}
Packit Service 3a6627
Packit 63bb0d
// WithPipeline creates a new ContainerURL object identical to the source but with the specified request policy pipeline.
Packit 63bb0d
func (c ContainerURL) WithPipeline(p pipeline.Pipeline) ContainerURL {
Packit 63bb0d
	return NewContainerURL(c.URL(), p)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// NewBlobURL creates a new BlobURL object by concatenating blobName to the end of
Packit 63bb0d
// ContainerURL's URL. The new BlobURL uses the same request policy pipeline as the ContainerURL.
Packit 63bb0d
// To change the pipeline, create the BlobURL and then call its WithPipeline method passing in the
Packit 63bb0d
// desired pipeline object. Or, call this package's NewBlobURL instead of calling this object's
Packit 63bb0d
// NewBlobURL method.
Packit 63bb0d
func (c ContainerURL) NewBlobURL(blobName string) BlobURL {
Packit 63bb0d
	blobURL := appendToURLPath(c.URL(), blobName)
Packit 63bb0d
	return NewBlobURL(blobURL, c.client.Pipeline())
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// NewAppendBlobURL creates a new AppendBlobURL object by concatenating blobName to the end of
Packit 63bb0d
// ContainerURL's URL. The new AppendBlobURL uses the same request policy pipeline as the ContainerURL.
Packit 63bb0d
// To change the pipeline, create the AppendBlobURL and then call its WithPipeline method passing in the
Packit 63bb0d
// desired pipeline object. Or, call this package's NewAppendBlobURL instead of calling this object's
Packit 63bb0d
// NewAppendBlobURL method.
Packit 63bb0d
func (c ContainerURL) NewAppendBlobURL(blobName string) AppendBlobURL {
Packit 63bb0d
	blobURL := appendToURLPath(c.URL(), blobName)
Packit 63bb0d
	return NewAppendBlobURL(blobURL, c.client.Pipeline())
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// NewBlockBlobURL creates a new BlockBlobURL object by concatenating blobName to the end of
Packit 63bb0d
// ContainerURL's URL. The new BlockBlobURL uses the same request policy pipeline as the ContainerURL.
Packit 63bb0d
// To change the pipeline, create the BlockBlobURL and then call its WithPipeline method passing in the
Packit 63bb0d
// desired pipeline object. Or, call this package's NewBlockBlobURL instead of calling this object's
Packit 63bb0d
// NewBlockBlobURL method.
Packit 63bb0d
func (c ContainerURL) NewBlockBlobURL(blobName string) BlockBlobURL {
Packit 63bb0d
	blobURL := appendToURLPath(c.URL(), blobName)
Packit 63bb0d
	return NewBlockBlobURL(blobURL, c.client.Pipeline())
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// NewPageBlobURL creates a new PageBlobURL object by concatenating blobName to the end of
Packit 63bb0d
// ContainerURL's URL. The new PageBlobURL uses the same request policy pipeline as the ContainerURL.
Packit 63bb0d
// To change the pipeline, create the PageBlobURL and then call its WithPipeline method passing in the
Packit 63bb0d
// desired pipeline object. Or, call this package's NewPageBlobURL instead of calling this object's
Packit 63bb0d
// NewPageBlobURL method.
Packit 63bb0d
func (c ContainerURL) NewPageBlobURL(blobName string) PageBlobURL {
Packit 63bb0d
	blobURL := appendToURLPath(c.URL(), blobName)
Packit 63bb0d
	return NewPageBlobURL(blobURL, c.client.Pipeline())
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Create creates a new container within a storage account. If a container with the same name already exists, the operation fails.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/create-container.
Packit 63bb0d
func (c ContainerURL) Create(ctx context.Context, metadata Metadata, publicAccessType PublicAccessType) (*ContainerCreateResponse, error) {
Packit Service 3a6627
	return c.client.Create(ctx, nil, metadata, publicAccessType, nil,
Packit Service 3a6627
		nil, nil, // container encryption
Packit Service 3a6627
	)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Delete marks the specified container for deletion. The container and any blobs contained within it are later deleted during garbage collection.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/delete-container.
Packit 63bb0d
func (c ContainerURL) Delete(ctx context.Context, ac ContainerAccessConditions) (*ContainerDeleteResponse, error) {
Packit 63bb0d
	if ac.IfMatch != ETagNone || ac.IfNoneMatch != ETagNone {
Packit 63bb0d
		return nil, errors.New("the IfMatch and IfNoneMatch access conditions must have their default values because they are ignored by the service")
Packit 63bb0d
	}
Packit 63bb0d
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, _, _ := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	return c.client.Delete(ctx, nil, ac.LeaseAccessConditions.pointers(),
Packit 63bb0d
		ifModifiedSince, ifUnmodifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// GetProperties returns the container's properties.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/get-container-metadata.
Packit 63bb0d
func (c ContainerURL) GetProperties(ctx context.Context, ac LeaseAccessConditions) (*ContainerGetPropertiesResponse, error) {
Packit 63bb0d
	// NOTE: GetMetadata actually calls GetProperties internally because GetProperties returns the metadata AND the properties.
Packit 63bb0d
	// This allows us to not expose a GetProperties method at all simplifying the API.
Packit 63bb0d
	return c.client.GetProperties(ctx, nil, ac.pointers(), nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// SetMetadata sets the container's metadata.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/set-container-metadata.
Packit 63bb0d
func (c ContainerURL) SetMetadata(ctx context.Context, metadata Metadata, ac ContainerAccessConditions) (*ContainerSetMetadataResponse, error) {
Packit 63bb0d
	if !ac.IfUnmodifiedSince.IsZero() || ac.IfMatch != ETagNone || ac.IfNoneMatch != ETagNone {
Packit 63bb0d
		return nil, errors.New("the IfUnmodifiedSince, IfMatch, and IfNoneMatch must have their default values because they are ignored by the blob service")
Packit 63bb0d
	}
Packit 63bb0d
	ifModifiedSince, _, _, _ := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	return c.client.SetMetadata(ctx, nil, ac.LeaseAccessConditions.pointers(), metadata, ifModifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// GetAccessPolicy returns the container's access policy. The access policy indicates whether container's blobs may be accessed publicly.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/get-container-acl.
Packit 63bb0d
func (c ContainerURL) GetAccessPolicy(ctx context.Context, ac LeaseAccessConditions) (*SignedIdentifiers, error) {
Packit 63bb0d
	return c.client.GetAccessPolicy(ctx, nil, ac.pointers(), nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// The AccessPolicyPermission type simplifies creating the permissions string for a container's access policy.
Packit 63bb0d
// Initialize an instance of this type and then call its String method to set AccessPolicy's Permission field.
Packit 63bb0d
type AccessPolicyPermission struct {
Packit 63bb0d
	Read, Add, Create, Write, Delete, List bool
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// String produces the access policy permission string for an Azure Storage container.
Packit 63bb0d
// Call this method to set AccessPolicy's Permission field.
Packit 63bb0d
func (p AccessPolicyPermission) String() string {
Packit 63bb0d
	var b bytes.Buffer
Packit 63bb0d
	if p.Read {
Packit 63bb0d
		b.WriteRune('r')
Packit 63bb0d
	}
Packit 63bb0d
	if p.Add {
Packit 63bb0d
		b.WriteRune('a')
Packit 63bb0d
	}
Packit 63bb0d
	if p.Create {
Packit 63bb0d
		b.WriteRune('c')
Packit 63bb0d
	}
Packit 63bb0d
	if p.Write {
Packit 63bb0d
		b.WriteRune('w')
Packit 63bb0d
	}
Packit 63bb0d
	if p.Delete {
Packit 63bb0d
		b.WriteRune('d')
Packit 63bb0d
	}
Packit 63bb0d
	if p.List {
Packit 63bb0d
		b.WriteRune('l')
Packit 63bb0d
	}
Packit 63bb0d
	return b.String()
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Parse initializes the AccessPolicyPermission's fields from a string.
Packit 63bb0d
func (p *AccessPolicyPermission) Parse(s string) error {
Packit 63bb0d
	*p = AccessPolicyPermission{} // Clear the flags
Packit 63bb0d
	for _, r := range s {
Packit 63bb0d
		switch r {
Packit 63bb0d
		case 'r':
Packit 63bb0d
			p.Read = true
Packit 63bb0d
		case 'a':
Packit 63bb0d
			p.Add = true
Packit 63bb0d
		case 'c':
Packit 63bb0d
			p.Create = true
Packit 63bb0d
		case 'w':
Packit 63bb0d
			p.Write = true
Packit 63bb0d
		case 'd':
Packit 63bb0d
			p.Delete = true
Packit 63bb0d
		case 'l':
Packit 63bb0d
			p.List = true
Packit 63bb0d
		default:
Packit 63bb0d
			return fmt.Errorf("invalid permission: '%v'", r)
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
	return nil
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// SetAccessPolicy sets the container's permissions. The access policy indicates whether blobs in a container may be accessed publicly.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/set-container-acl.
Packit 63bb0d
func (c ContainerURL) SetAccessPolicy(ctx context.Context, accessType PublicAccessType, si []SignedIdentifier,
Packit 63bb0d
	ac ContainerAccessConditions) (*ContainerSetAccessPolicyResponse, error) {
Packit 63bb0d
	if ac.IfMatch != ETagNone || ac.IfNoneMatch != ETagNone {
Packit 63bb0d
		return nil, errors.New("the IfMatch and IfNoneMatch access conditions must have their default values because they are ignored by the service")
Packit 63bb0d
	}
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, _, _ := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	return c.client.SetAccessPolicy(ctx, si, nil, ac.LeaseAccessConditions.pointers(),
Packit 63bb0d
		accessType, ifModifiedSince, ifUnmodifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// AcquireLease acquires a lease on the container for delete operations. The lease duration must be between 15 to 60 seconds, or infinite (-1).
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-container.
Packit 63bb0d
func (c ContainerURL) AcquireLease(ctx context.Context, proposedID string, duration int32, ac ModifiedAccessConditions) (*ContainerAcquireLeaseResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, _, _ := ac.pointers()
Packit 63bb0d
	return c.client.AcquireLease(ctx, nil, &duration, &proposedID,
Packit 63bb0d
		ifModifiedSince, ifUnmodifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// RenewLease renews the container's previously-acquired lease.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-container.
Packit 63bb0d
func (c ContainerURL) RenewLease(ctx context.Context, leaseID string, ac ModifiedAccessConditions) (*ContainerRenewLeaseResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, _, _ := ac.pointers()
Packit 63bb0d
	return c.client.RenewLease(ctx, leaseID, nil, ifModifiedSince, ifUnmodifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// ReleaseLease releases the container's previously-acquired lease.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-container.
Packit 63bb0d
func (c ContainerURL) ReleaseLease(ctx context.Context, leaseID string, ac ModifiedAccessConditions) (*ContainerReleaseLeaseResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, _, _ := ac.pointers()
Packit 63bb0d
	return c.client.ReleaseLease(ctx, leaseID, nil, ifModifiedSince, ifUnmodifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// BreakLease breaks the container's previously-acquired lease (if it exists).
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-container.
Packit 63bb0d
func (c ContainerURL) BreakLease(ctx context.Context, period int32, ac ModifiedAccessConditions) (*ContainerBreakLeaseResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, _, _ := ac.pointers()
Packit 63bb0d
	return c.client.BreakLease(ctx, nil, leasePeriodPointer(period), ifModifiedSince, ifUnmodifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// ChangeLease changes the container's lease ID.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/lease-container.
Packit 63bb0d
func (c ContainerURL) ChangeLease(ctx context.Context, leaseID string, proposedID string, ac ModifiedAccessConditions) (*ContainerChangeLeaseResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, _, _ := ac.pointers()
Packit 63bb0d
	return c.client.ChangeLease(ctx, leaseID, proposedID, nil, ifModifiedSince, ifUnmodifiedSince, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// ListBlobsFlatSegment returns a single segment of blobs starting from the specified Marker. Use an empty
Packit 63bb0d
// Marker to start enumeration from the beginning. Blob names are returned in lexicographic order.
Packit 63bb0d
// After getting a segment, process it, and then call ListBlobsFlatSegment again (passing the the
Packit 63bb0d
// previously-returned Marker) to get the next segment.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/list-blobs.
Packit 63bb0d
func (c ContainerURL) ListBlobsFlatSegment(ctx context.Context, marker Marker, o ListBlobsSegmentOptions) (*ListBlobsFlatSegmentResponse, error) {
Packit 63bb0d
	prefix, include, maxResults := o.pointers()
Packit 63bb0d
	return c.client.ListBlobFlatSegment(ctx, prefix, marker.Val, maxResults, include, nil, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// ListBlobsHierarchySegment returns a single segment of blobs starting from the specified Marker. Use an empty
Packit 63bb0d
// Marker to start enumeration from the beginning. Blob names are returned in lexicographic order.
Packit 63bb0d
// After getting a segment, process it, and then call ListBlobsHierarchicalSegment again (passing the the
Packit 63bb0d
// previously-returned Marker) to get the next segment.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/list-blobs.
Packit 63bb0d
func (c ContainerURL) ListBlobsHierarchySegment(ctx context.Context, marker Marker, delimiter string, o ListBlobsSegmentOptions) (*ListBlobsHierarchySegmentResponse, error) {
Packit 63bb0d
	if o.Details.Snapshots {
Packit 63bb0d
		return nil, errors.New("snapshots are not supported in this listing operation")
Packit 63bb0d
	}
Packit 63bb0d
	prefix, include, maxResults := o.pointers()
Packit 63bb0d
	return c.client.ListBlobHierarchySegment(ctx, delimiter, prefix, marker.Val, maxResults, include, nil, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// ListBlobsSegmentOptions defines options available when calling ListBlobs.
Packit 63bb0d
type ListBlobsSegmentOptions struct {
Packit 63bb0d
	Details BlobListingDetails // No IncludeType header is produced if ""
Packit 63bb0d
	Prefix  string             // No Prefix header is produced if ""
Packit 63bb0d
Packit 63bb0d
	// SetMaxResults sets the maximum desired results you want the service to return. Note, the
Packit 63bb0d
	// service may return fewer results than requested.
Packit 63bb0d
	// MaxResults=0 means no 'MaxResults' header specified.
Packit 63bb0d
	MaxResults int32
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
func (o *ListBlobsSegmentOptions) pointers() (prefix *string, include []ListBlobsIncludeItemType, maxResults *int32) {
Packit 63bb0d
	if o.Prefix != "" {
Packit 63bb0d
		prefix = &o.Prefix
Packit 63bb0d
	}
Packit 63bb0d
	include = o.Details.slice()
Packit 63bb0d
	if o.MaxResults != 0 {
Packit 63bb0d
		maxResults = &o.MaxResults
Packit 63bb0d
	}
Packit 63bb0d
	return
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// BlobListingDetails indicates what additional information the service should return with each blob.
Packit 63bb0d
type BlobListingDetails struct {
Packit Service 3a6627
	Copy, Metadata, Snapshots, UncommittedBlobs, Deleted, Tags, Versions bool
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// string produces the Include query parameter's value.
Packit 63bb0d
func (d *BlobListingDetails) slice() []ListBlobsIncludeItemType {
Packit 63bb0d
	items := []ListBlobsIncludeItemType{}
Packit 63bb0d
	// NOTE: Multiple strings MUST be appended in alphabetic order or signing the string for authentication fails!
Packit 63bb0d
	if d.Copy {
Packit 63bb0d
		items = append(items, ListBlobsIncludeItemCopy)
Packit 63bb0d
	}
Packit 63bb0d
	if d.Deleted {
Packit 63bb0d
		items = append(items, ListBlobsIncludeItemDeleted)
Packit 63bb0d
	}
Packit 63bb0d
	if d.Metadata {
Packit 63bb0d
		items = append(items, ListBlobsIncludeItemMetadata)
Packit 63bb0d
	}
Packit 63bb0d
	if d.Snapshots {
Packit 63bb0d
		items = append(items, ListBlobsIncludeItemSnapshots)
Packit 63bb0d
	}
Packit 63bb0d
	if d.UncommittedBlobs {
Packit 63bb0d
		items = append(items, ListBlobsIncludeItemUncommittedblobs)
Packit 63bb0d
	}
Packit Service 3a6627
	if d.Tags {
Packit Service 3a6627
		items = append(items, ListBlobsIncludeItemTags)
Packit Service 3a6627
	}
Packit Service 3a6627
	if d.Versions {
Packit Service 3a6627
		items = append(items, ListBlobsIncludeItemVersions)
Packit Service 3a6627
	}
Packit 63bb0d
	return items
Packit 63bb0d
}