forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemention ShareFileStorageResource (Azure#38938)
* WIP * Implemented base file storage resource; Testst * Export API * Attempt to fix double dependency files in tests * Addressing PR comments; Added StorageResourceItemInternal * Rename internal methods * PR Comments for tests * CHange out verify parameters with the mock verify method
- Loading branch information
1 parent
5097030
commit c21559a
Showing
18 changed files
with
1,012 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/DataMovementShareConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Azure.Storage.DataMovement.Files.Shares | ||
{ | ||
internal class DataMovementShareConstants | ||
{ | ||
public const int KB = 1024; | ||
public const int MB = KB * 1024; | ||
|
||
internal const int MaxRange = 4 * MB; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/DataMovementSharesExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure.Storage.Files.Shares.Models; | ||
|
||
namespace Azure.Storage.DataMovement.Files.Shares | ||
{ | ||
internal static partial class DataMovementSharesExtensions | ||
{ | ||
internal static ShareFileUploadOptions ToShareFileUploadOptions( | ||
this ShareFileStorageResourceOptions options) | ||
=> new() | ||
{ | ||
Conditions = options?.DestinationConditions, | ||
TransferValidation = options?.UploadTransferValidationOptions, | ||
}; | ||
|
||
internal static ShareFileUploadRangeOptions ToShareFileUploadRangeOptions( | ||
this ShareFileStorageResourceOptions options) | ||
=> new() | ||
{ | ||
Conditions = options?.DestinationConditions, | ||
TransferValidation = options?.UploadTransferValidationOptions, | ||
}; | ||
|
||
internal static ShareFileUploadRangeFromUriOptions ToShareFileUploadRangeFromUriOptions( | ||
this ShareFileStorageResourceOptions options) | ||
=> new() | ||
{ | ||
Conditions = options?.DestinationConditions, | ||
}; | ||
|
||
internal static StorageResourceProperties ToStorageResourceProperties( | ||
this ShareFileProperties properties) | ||
=> new( | ||
lastModified: properties.LastModified, | ||
createdOn: properties?.SmbProperties?.FileCreatedOn ?? default, | ||
metadata: properties?.Metadata, | ||
copyCompletedOn: properties.CopyCompletedOn, | ||
copyStatusDescription: properties?.CopyStatusDescription, | ||
copyId: properties?.CopyId, | ||
copyProgress: properties?.CopyProgress, | ||
copySource: new Uri(properties?.CopySource), | ||
contentLength: properties.ContentLength, | ||
contentType: properties?.ContentType, | ||
eTag: properties.ETag, | ||
contentHash: properties?.ContentHash, | ||
blobSequenceNumber: default, | ||
blobCommittedBlockCount: default, | ||
isServerEncrypted: properties.IsServerEncrypted, | ||
encryptionKeySha256: default, | ||
encryptionScope: default, | ||
versionId: default, | ||
isLatestVersion: default, | ||
expiresOn: default, | ||
lastAccessed: default); | ||
|
||
internal static ShareFileDownloadOptions ToShareFileDownloadOptions( | ||
this ShareFileStorageResourceOptions options, | ||
HttpRange range) | ||
=> new() | ||
{ | ||
Range = range, | ||
Conditions = options?.SourceConditions, | ||
TransferValidation = options?.DownloadTransferValidationOptions, | ||
}; | ||
|
||
internal static StorageResourceReadStreamResult ToStorageResourceReadStreamResult( | ||
this ShareFileDownloadInfo info) | ||
=> new( | ||
content: info?.Content, | ||
contentRange: info?.Details.ContentRange, | ||
acceptRanges: info?.Details.AcceptRanges, | ||
rangeContentHash: info?.Details.FileContentHash, | ||
properties: info?.Details.ToStorageResourceProperties()); | ||
|
||
private static StorageResourceProperties ToStorageResourceProperties( | ||
this ShareFileDownloadDetails details) | ||
=> new( | ||
lastModified: details.LastModified, | ||
createdOn: details.SmbProperties.FileCreatedOn ?? default, | ||
metadata: details.Metadata, | ||
copyCompletedOn: details.CopyCompletedOn, | ||
copyStatusDescription: details.CopyStatusDescription, | ||
copyId: details.CopyId, | ||
copyProgress: details.CopyProgress, | ||
copySource: details.CopySource, | ||
contentLength: details.ContentRange.Length, | ||
contentType: default, | ||
eTag: details.ETag, | ||
contentHash: default, | ||
blobSequenceNumber: default, | ||
blobCommittedBlockCount: default, | ||
isServerEncrypted: details.IsServerEncrypted, | ||
encryptionKeySha256: default, | ||
encryptionScope: default, | ||
versionId: default, | ||
isLatestVersion: default, | ||
expiresOn: default, | ||
lastAccessed: default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
sdk/storage/Azure.Storage.DataMovement.Files.Shares/src/ShareFileStorageResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Storage.Files.Shares; | ||
using Azure.Storage.Files.Shares.Models; | ||
|
||
namespace Azure.Storage.DataMovement.Files.Shares | ||
{ | ||
internal class ShareFileStorageResource : StorageResourceItemInternal | ||
{ | ||
internal long? _length; | ||
internal readonly ShareFileStorageResourceOptions _options; | ||
internal ETag? _etagDownloadLock = default; | ||
|
||
internal ShareFileClient ShareFileClient { get; } | ||
|
||
public override Uri Uri => ShareFileClient.Uri; | ||
|
||
protected override string ResourceId => "ShareFile"; | ||
|
||
protected override DataTransferOrder TransferType => DataTransferOrder.Sequential; | ||
|
||
protected override long MaxChunkSize => DataMovementShareConstants.MaxRange; | ||
|
||
protected override long? Length => _length; | ||
|
||
public ShareFileStorageResource( | ||
ShareFileClient fileClient, | ||
ShareFileStorageResourceOptions options = default) | ||
{ | ||
ShareFileClient = fileClient; | ||
_options = options; | ||
} | ||
|
||
/// <summary> | ||
/// Internal Constructor for constructing the resource retrieved by a GetStorageResources. | ||
/// </summary> | ||
/// <param name="fileClient">The blob client which will service the storage resource operations.</param> | ||
/// <param name="length">The content length of the blob.</param> | ||
/// <param name="etagLock">Preset etag to lock on for reads.</param> | ||
/// <param name="options">Options for the storage resource. See <see cref="ShareFileStorageResourceOptions"/>.</param> | ||
internal ShareFileStorageResource( | ||
ShareFileClient fileClient, | ||
long? length, | ||
ETag? etagLock, | ||
ShareFileStorageResourceOptions options = default) | ||
: this(fileClient, options) | ||
{ | ||
_length = length; | ||
_etagDownloadLock = etagLock; | ||
} | ||
|
||
protected override Task CompleteTransferAsync( | ||
bool overwrite, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
return Task.CompletedTask; | ||
} | ||
|
||
protected override async Task CopyBlockFromUriAsync( | ||
StorageResourceItem sourceResource, | ||
HttpRange range, | ||
bool overwrite, | ||
long completeLength, | ||
StorageResourceCopyFromUriOptions options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
await ShareFileClient.UploadRangeFromUriAsync( | ||
sourceUri: sourceResource.Uri, | ||
range: range, | ||
sourceRange: range, | ||
options: _options.ToShareFileUploadRangeFromUriOptions(), | ||
cancellationToken: cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
protected override async Task CopyFromStreamAsync( | ||
Stream stream, | ||
long streamLength, | ||
bool overwrite, | ||
long completeLength, | ||
StorageResourceWriteToOffsetOptions options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
|
||
long position = options?.Position != default ? options.Position.Value : 0; | ||
if ((streamLength == completeLength) && position == 0) | ||
{ | ||
// Default to Upload | ||
await ShareFileClient.UploadAsync( | ||
stream, | ||
_options.ToShareFileUploadOptions(), | ||
cancellationToken: cancellationToken).ConfigureAwait(false); | ||
return; | ||
} | ||
|
||
// Otherwise upload the Range | ||
await ShareFileClient.UploadRangeAsync( | ||
new HttpRange(position, streamLength), | ||
stream, | ||
_options.ToShareFileUploadRangeOptions(), | ||
cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
protected override async Task CopyFromUriAsync( | ||
StorageResourceItem sourceResource, | ||
bool overwrite, | ||
long completeLength, | ||
StorageResourceCopyFromUriOptions options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
await ShareFileClient.UploadRangeFromUriAsync( | ||
sourceUri: sourceResource.Uri, | ||
range: new HttpRange(0, completeLength), | ||
sourceRange: new HttpRange(0, completeLength), | ||
options: _options.ToShareFileUploadRangeFromUriOptions(), | ||
cancellationToken: cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
protected override async Task<bool> DeleteIfExistsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
return await ShareFileClient.DeleteIfExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
protected override Task<HttpAuthorization> GetCopyAuthorizationHeaderAsync(CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
// TODO: This needs an update to ShareFileClient to allow getting the Copy Authorization Token | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override async Task<StorageResourceProperties> GetPropertiesAsync(CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
Response<ShareFileProperties> response = await ShareFileClient.GetPropertiesAsync( | ||
conditions: _options?.SourceConditions, | ||
cancellationToken: cancellationToken).ConfigureAwait(false); | ||
// TODO: should we be grabbing the ETag here even though we can't apply it to the download. | ||
//GrabEtag(response.GetRawResponse()); | ||
return response.Value.ToStorageResourceProperties(); | ||
} | ||
|
||
protected override async Task<StorageResourceReadStreamResult> ReadStreamAsync( | ||
long position = 0, | ||
long? length = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
CancellationHelper.ThrowIfCancellationRequested(cancellationToken); | ||
Response<ShareFileDownloadInfo> response = await ShareFileClient.DownloadAsync( | ||
_options.ToShareFileDownloadOptions(new HttpRange(position, length)), | ||
cancellationToken).ConfigureAwait(false); | ||
return response.Value.ToStorageResourceReadStreamResult(); | ||
} | ||
|
||
protected override StorageResourceCheckpointData GetSourceCheckpointData() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override StorageResourceCheckpointData GetDestinationCheckpointData() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
Oops, something went wrong.