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

Packit 63bb0d
package azblob
Packit 63bb0d
Packit 63bb0d
import (
Packit 63bb0d
	"context"
Packit 63bb0d
	"fmt"
Packit 63bb0d
	"io"
Packit 63bb0d
	"net/url"
Packit 63bb0d
	"strconv"
Packit 63bb0d
Packit 63bb0d
	"github.com/Azure/azure-pipeline-go/pipeline"
Packit 63bb0d
)
Packit 63bb0d
Packit 63bb0d
const (
Packit 63bb0d
	// PageBlobPageBytes indicates the number of bytes in a page (512).
Packit 63bb0d
	PageBlobPageBytes = 512
Packit 63bb0d
Packit Service 3a6627
	// PageBlobMaxUploadPagesBytes indicates the maximum number of bytes that can be sent in a call to PutPage.
Packit 63bb0d
	PageBlobMaxUploadPagesBytes = 4 * 1024 * 1024 // 4MB
Packit 63bb0d
)
Packit 63bb0d
Packit 63bb0d
// PageBlobURL defines a set of operations applicable to page blobs.
Packit 63bb0d
type PageBlobURL struct {
Packit 63bb0d
	BlobURL
Packit 63bb0d
	pbClient pageBlobClient
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// NewPageBlobURL creates a PageBlobURL object using the specified URL and request policy pipeline.
Packit 63bb0d
func NewPageBlobURL(url url.URL, p pipeline.Pipeline) PageBlobURL {
Packit 63bb0d
	blobClient := newBlobClient(url, p)
Packit 63bb0d
	pbClient := newPageBlobClient(url, p)
Packit 63bb0d
	return PageBlobURL{BlobURL: BlobURL{blobClient: blobClient}, pbClient: pbClient}
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// WithPipeline creates a new PageBlobURL object identical to the source but with the specific request policy pipeline.
Packit 63bb0d
func (pb PageBlobURL) WithPipeline(p pipeline.Pipeline) PageBlobURL {
Packit 63bb0d
	return NewPageBlobURL(pb.blobClient.URL(), p)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// WithSnapshot creates a new PageBlobURL object identical to the source but with the specified snapshot timestamp.
Packit 63bb0d
// Pass "" to remove the snapshot returning a URL to the base blob.
Packit 63bb0d
func (pb PageBlobURL) WithSnapshot(snapshot string) PageBlobURL {
Packit 63bb0d
	p := NewBlobURLParts(pb.URL())
Packit 63bb0d
	p.Snapshot = snapshot
Packit 63bb0d
	return NewPageBlobURL(p.URL(), pb.blobClient.Pipeline())
Packit 63bb0d
}
Packit 63bb0d
Packit Service 3a6627
// WithVersionID creates a new PageBlobURL object identical to the source but with the specified snapshot timestamp.
Packit Service 3a6627
// Pass "" to remove the snapshot returning a URL to the base blob.
Packit Service 3a6627
func (pb PageBlobURL) WithVersionID(versionId string) PageBlobURL {
Packit Service 3a6627
	p := NewBlobURLParts(pb.URL())
Packit Service 3a6627
	p.VersionID = versionId
Packit Service 3a6627
	return NewPageBlobURL(p.URL(), pb.blobClient.Pipeline())
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
func (pb PageBlobURL) GetAccountInfo(ctx context.Context) (*BlobGetAccountInfoResponse, error) {
Packit Service 3a6627
	return pb.blobClient.GetAccountInfo(ctx)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// Create creates a page blob of the specified length. Call PutPage to upload data to a page blob.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/put-blob.
Packit Service 3a6627
func (pb PageBlobURL) Create(ctx context.Context, size int64, sequenceNumber int64, h BlobHTTPHeaders, metadata Metadata, ac BlobAccessConditions, tier PremiumPageBlobAccessTierType, blobTagsMap BlobTagsMap, cpk ClientProvidedKeyOptions) (*PageBlobCreateResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit Service 3a6627
	blobTagsString := SerializeBlobTagsHeader(blobTagsMap)
Packit Service 3a6627
	return pb.pbClient.Create(ctx, 0, size, nil, tier,
Packit 63bb0d
		&h.ContentType, &h.ContentEncoding, &h.ContentLanguage, h.ContentMD5, &h.CacheControl,
Packit Service 3a6627
		metadata, ac.LeaseAccessConditions.pointers(), &h.ContentDisposition,
Packit Service 3a6627
		cpk.EncryptionKey, cpk.EncryptionKeySha256, cpk.EncryptionAlgorithm, // CPK-V
Packit Service 3a6627
		cpk.EncryptionScope, // CPK-N
Packit Service 3a6627
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag,
Packit Service 3a6627
		nil, // Blob tags
Packit Service 3a6627
		&sequenceNumber, nil,
Packit Service 3a6627
		blobTagsString, // Blob tags
Packit Service 3a6627
	)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// UploadPages writes 1 or more pages to the page blob. The start offset and the stream size must be a multiple of 512 bytes.
Packit 63bb0d
// This method panics if the stream is not at position 0.
Packit 63bb0d
// Note that the http client closes the body stream after the request is sent to the service.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/put-page.
Packit Service 3a6627
func (pb PageBlobURL) UploadPages(ctx context.Context, offset int64, body io.ReadSeeker, ac PageBlobAccessConditions, transactionalMD5 []byte, cpk ClientProvidedKeyOptions) (*PageBlobUploadPagesResponse, error) {
Packit 63bb0d
	count, err := validateSeekableStreamAt0AndGetCount(body)
Packit 63bb0d
	if err != nil {
Packit 63bb0d
		return nil, err
Packit 63bb0d
	}
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	ifSequenceNumberLessThanOrEqual, ifSequenceNumberLessThan, ifSequenceNumberEqual := ac.SequenceNumberAccessConditions.pointers()
Packit Service 3a6627
	return pb.pbClient.UploadPages(ctx, body, count, transactionalMD5, nil, nil,
Packit 63bb0d
		PageRange{Start: offset, End: offset + count - 1}.pointers(),
Packit 63bb0d
		ac.LeaseAccessConditions.pointers(),
Packit Service 3a6627
		cpk.EncryptionKey, cpk.EncryptionKeySha256, cpk.EncryptionAlgorithm, // CPK
Packit Service 3a6627
		cpk.EncryptionScope, // CPK-N
Packit 63bb0d
		ifSequenceNumberLessThanOrEqual, ifSequenceNumberLessThan, ifSequenceNumberEqual,
Packit Service 3a6627
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag,
Packit Service 3a6627
		nil, // Blob ifTags
Packit Service 3a6627
		nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// UploadPagesFromURL copies 1 or more pages from a source URL to the page blob.
Packit 63bb0d
// The sourceOffset specifies the start offset of source data to copy from.
Packit 63bb0d
// The destOffset specifies the start offset of data in page blob will be written to.
Packit 63bb0d
// The count must be a multiple of 512 bytes.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/put-page-from-url.
Packit Service 3a6627
func (pb PageBlobURL) UploadPagesFromURL(ctx context.Context, sourceURL url.URL, sourceOffset int64, destOffset int64, count int64, transactionalMD5 []byte, destinationAccessConditions PageBlobAccessConditions, sourceAccessConditions ModifiedAccessConditions, cpk ClientProvidedKeyOptions) (*PageBlobUploadPagesFromURLResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := destinationAccessConditions.ModifiedAccessConditions.pointers()
Packit 63bb0d
	sourceIfModifiedSince, sourceIfUnmodifiedSince, sourceIfMatchETag, sourceIfNoneMatchETag := sourceAccessConditions.pointers()
Packit 63bb0d
	ifSequenceNumberLessThanOrEqual, ifSequenceNumberLessThan, ifSequenceNumberEqual := destinationAccessConditions.SequenceNumberAccessConditions.pointers()
Packit 63bb0d
	return pb.pbClient.UploadPagesFromURL(ctx, sourceURL.String(), *PageRange{Start: sourceOffset, End: sourceOffset + count - 1}.pointers(), 0,
Packit Service 3a6627
		*PageRange{Start: destOffset, End: destOffset + count - 1}.pointers(), transactionalMD5, nil, nil,
Packit Service 3a6627
		cpk.EncryptionKey, cpk.EncryptionKeySha256, cpk.EncryptionAlgorithm, // CPK-V
Packit Service 3a6627
		cpk.EncryptionScope, // CPK-N
Packit Service 3a6627
		destinationAccessConditions.LeaseAccessConditions.pointers(),
Packit 63bb0d
		ifSequenceNumberLessThanOrEqual, ifSequenceNumberLessThan, ifSequenceNumberEqual,
Packit Service 3a6627
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag,
Packit Service 3a6627
		nil, // Blob ifTags
Packit Service 3a6627
		sourceIfModifiedSince, sourceIfUnmodifiedSince, sourceIfMatchETag, sourceIfNoneMatchETag, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// ClearPages frees the specified pages from the page blob.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/put-page.
Packit Service 3a6627
func (pb PageBlobURL) ClearPages(ctx context.Context, offset int64, count int64, ac PageBlobAccessConditions, cpk ClientProvidedKeyOptions) (*PageBlobClearPagesResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	ifSequenceNumberLessThanOrEqual, ifSequenceNumberLessThan, ifSequenceNumberEqual := ac.SequenceNumberAccessConditions.pointers()
Packit 63bb0d
	return pb.pbClient.ClearPages(ctx, 0, nil,
Packit 63bb0d
		PageRange{Start: offset, End: offset + count - 1}.pointers(),
Packit 63bb0d
		ac.LeaseAccessConditions.pointers(),
Packit Service 3a6627
		cpk.EncryptionKey, cpk.EncryptionKeySha256, cpk.EncryptionAlgorithm, // CPK
Packit Service 3a6627
		cpk.EncryptionScope, // CPK-N
Packit 63bb0d
		ifSequenceNumberLessThanOrEqual, ifSequenceNumberLessThan,
Packit 63bb0d
		ifSequenceNumberEqual, ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// GetPageRanges returns the list of valid page ranges for a page blob or snapshot of a page blob.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/get-page-ranges.
Packit 63bb0d
func (pb PageBlobURL) GetPageRanges(ctx context.Context, offset int64, count int64, ac BlobAccessConditions) (*PageList, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	return pb.pbClient.GetPageRanges(ctx, nil, nil,
Packit 63bb0d
		httpRange{offset: offset, count: count}.pointers(),
Packit 63bb0d
		ac.LeaseAccessConditions.pointers(),
Packit Service 3a6627
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag,
Packit Service 3a6627
		nil, // Blob ifTags
Packit Service 3a6627
		nil)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// GetManagedDiskPageRangesDiff gets the collection of page ranges that differ between a specified snapshot and this page blob representing managed disk.
Packit Service 3a6627
// For more information, see https://docs.microsoft.com/rest/api/storageservices/get-page-ranges.
Packit Service 3a6627
func (pb PageBlobURL) GetManagedDiskPageRangesDiff(ctx context.Context, offset int64, count int64, prevSnapshot *string, prevSnapshotURL *string, ac BlobAccessConditions) (*PageList, error) {
Packit Service 3a6627
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit Service 3a6627
Packit Service 3a6627
	return pb.pbClient.GetPageRangesDiff(ctx, nil, nil, prevSnapshot,
Packit Service 3a6627
		prevSnapshotURL, // Get managed disk diff
Packit Service 3a6627
		httpRange{offset: offset, count: count}.pointers(),
Packit Service 3a6627
		ac.LeaseAccessConditions.pointers(),
Packit Service 3a6627
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag,
Packit Service 3a6627
		nil, // Blob ifTags
Packit Service 3a6627
		nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// GetPageRangesDiff gets the collection of page ranges that differ between a specified snapshot and this page blob.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/get-page-ranges.
Packit 63bb0d
func (pb PageBlobURL) GetPageRangesDiff(ctx context.Context, offset int64, count int64, prevSnapshot string, ac BlobAccessConditions) (*PageList, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	return pb.pbClient.GetPageRangesDiff(ctx, nil, nil, &prevSnapshot,
Packit Service 3a6627
		nil, // Get managed disk diff
Packit 63bb0d
		httpRange{offset: offset, count: count}.pointers(),
Packit 63bb0d
		ac.LeaseAccessConditions.pointers(),
Packit 63bb0d
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag,
Packit Service 3a6627
		nil, // Blob ifTags
Packit 63bb0d
		nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Resize resizes the page blob to the specified size (which must be a multiple of 512).
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/set-blob-properties.
Packit Service 3a6627
func (pb PageBlobURL) Resize(ctx context.Context, size int64, ac BlobAccessConditions, cpk ClientProvidedKeyOptions) (*PageBlobResizeResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	return pb.pbClient.Resize(ctx, size, nil, ac.LeaseAccessConditions.pointers(),
Packit Service 3a6627
		cpk.EncryptionKey, cpk.EncryptionKeySha256, cpk.EncryptionAlgorithm, // CPK
Packit Service 3a6627
		cpk.EncryptionScope, // CPK-N
Packit 63bb0d
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit Service 3a6627
// UpdateSequenceNumber sets the page blob's sequence number.
Packit 63bb0d
func (pb PageBlobURL) UpdateSequenceNumber(ctx context.Context, action SequenceNumberActionType, sequenceNumber int64,
Packit 63bb0d
	ac BlobAccessConditions) (*PageBlobUpdateSequenceNumberResponse, error) {
Packit 63bb0d
	sn := &sequenceNumber
Packit 63bb0d
	if action == SequenceNumberActionIncrement {
Packit 63bb0d
		sn = nil
Packit 63bb0d
	}
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatch, ifNoneMatch := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	return pb.pbClient.UpdateSequenceNumber(ctx, action, nil,
Packit 63bb0d
		ac.LeaseAccessConditions.pointers(), ifModifiedSince, ifUnmodifiedSince, ifMatch, ifNoneMatch,
Packit 63bb0d
		sn, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit Service 3a6627
// StartCopyIncremental begins an operation to start an incremental copy from one page blob's snapshot to this page blob.
Packit 63bb0d
// The snapshot is copied such that only the differential changes between the previously copied snapshot are transferred to the destination.
Packit 63bb0d
// The copied snapshots are complete copies of the original snapshot and can be read or copied from as usual.
Packit 63bb0d
// For more information, see https://docs.microsoft.com/rest/api/storageservices/incremental-copy-blob and
Packit 63bb0d
// https://docs.microsoft.com/en-us/azure/virtual-machines/windows/incremental-snapshots.
Packit 63bb0d
func (pb PageBlobURL) StartCopyIncremental(ctx context.Context, source url.URL, snapshot string, ac BlobAccessConditions) (*PageBlobCopyIncrementalResponse, error) {
Packit 63bb0d
	ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag := ac.ModifiedAccessConditions.pointers()
Packit 63bb0d
	qp := source.Query()
Packit 63bb0d
	qp.Set("snapshot", snapshot)
Packit 63bb0d
	source.RawQuery = qp.Encode()
Packit 63bb0d
	return pb.pbClient.CopyIncremental(ctx, source.String(), nil,
Packit 63bb0d
		ifModifiedSince, ifUnmodifiedSince, ifMatchETag, ifNoneMatchETag, nil)
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
func (pr PageRange) pointers() *string {
Packit 63bb0d
	endOffset := strconv.FormatInt(int64(pr.End), 10)
Packit 63bb0d
	asString := fmt.Sprintf("bytes=%v-%s", pr.Start, endOffset)
Packit 63bb0d
	return &asString
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
type PageBlobAccessConditions struct {
Packit 63bb0d
	ModifiedAccessConditions
Packit 63bb0d
	LeaseAccessConditions
Packit 63bb0d
	SequenceNumberAccessConditions
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// SequenceNumberAccessConditions identifies page blob-specific access conditions which you optionally set.
Packit 63bb0d
type SequenceNumberAccessConditions struct {
Packit 63bb0d
	// IfSequenceNumberLessThan ensures that the page blob operation succeeds
Packit 63bb0d
	// only if the blob's sequence number is less than a value.
Packit 63bb0d
	// IfSequenceNumberLessThan=0 means no 'IfSequenceNumberLessThan' header specified.
Packit 63bb0d
	// IfSequenceNumberLessThan>0 means 'IfSequenceNumberLessThan' header specified with its value
Packit 63bb0d
	// IfSequenceNumberLessThan==-1 means 'IfSequenceNumberLessThan' header specified with a value of 0
Packit 63bb0d
	IfSequenceNumberLessThan int64
Packit 63bb0d
Packit 63bb0d
	// IfSequenceNumberLessThanOrEqual ensures that the page blob operation succeeds
Packit 63bb0d
	// only if the blob's sequence number is less than or equal to a value.
Packit 63bb0d
	// IfSequenceNumberLessThanOrEqual=0 means no 'IfSequenceNumberLessThanOrEqual' header specified.
Packit 63bb0d
	// IfSequenceNumberLessThanOrEqual>0 means 'IfSequenceNumberLessThanOrEqual' header specified with its value
Packit 63bb0d
	// IfSequenceNumberLessThanOrEqual=-1 means 'IfSequenceNumberLessThanOrEqual' header specified with a value of 0
Packit 63bb0d
	IfSequenceNumberLessThanOrEqual int64
Packit 63bb0d
Packit 63bb0d
	// IfSequenceNumberEqual ensures that the page blob operation succeeds
Packit 63bb0d
	// only if the blob's sequence number is equal to a value.
Packit 63bb0d
	// IfSequenceNumberEqual=0 means no 'IfSequenceNumberEqual' header specified.
Packit 63bb0d
	// IfSequenceNumberEqual>0 means 'IfSequenceNumberEqual' header specified with its value
Packit 63bb0d
	// IfSequenceNumberEqual=-1 means 'IfSequenceNumberEqual' header specified with a value of 0
Packit 63bb0d
	IfSequenceNumberEqual int64
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// pointers is for internal infrastructure. It returns the fields as pointers.
Packit 63bb0d
func (ac SequenceNumberAccessConditions) pointers() (snltoe *int64, snlt *int64, sne *int64) {
Packit 63bb0d
	var zero int64 // Defaults to 0
Packit 63bb0d
	switch ac.IfSequenceNumberLessThan {
Packit 63bb0d
	case -1:
Packit 63bb0d
		snlt = &zero
Packit 63bb0d
	case 0:
Packit 63bb0d
		snlt = nil
Packit 63bb0d
	default:
Packit 63bb0d
		snlt = &ac.IfSequenceNumberLessThan
Packit 63bb0d
	}
Packit 63bb0d
Packit 63bb0d
	switch ac.IfSequenceNumberLessThanOrEqual {
Packit 63bb0d
	case -1:
Packit 63bb0d
		snltoe = &zero
Packit 63bb0d
	case 0:
Packit 63bb0d
		snltoe = nil
Packit 63bb0d
	default:
Packit 63bb0d
		snltoe = &ac.IfSequenceNumberLessThanOrEqual
Packit 63bb0d
	}
Packit 63bb0d
	switch ac.IfSequenceNumberEqual {
Packit 63bb0d
	case -1:
Packit 63bb0d
		sne = &zero
Packit 63bb0d
	case 0:
Packit 63bb0d
		sne = nil
Packit 63bb0d
	default:
Packit 63bb0d
		sne = &ac.IfSequenceNumberEqual
Packit 63bb0d
	}
Packit 63bb0d
	return
Packit 63bb0d
}