Ejemplo n.º 1
0
        private async Task StartAsyncCopy(long taskId, CloudFile destFile, Func <bool> checkOverwrite, Func <Task <string> > startCopy)
        {
            bool destExist = true;

            try
            {
                await destFile.FetchAttributesAsync(null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false);

                //Clean the Metadata of the destination file object, or the source metadata won't overwirte the dest file metadata. See https://docs.microsoft.com/en-us/rest/api/storageservices/copy-file
                destFile.Metadata.Clear();
            }
            catch (StorageException ex)
            {
                if (!ex.IsNotFoundException())
                {
                    throw;
                }

                destExist = false;
            }

            if (!destExist || checkOverwrite())
            {
                string copyId = await startCopy().ConfigureAwait(false);

                this.OutputStream.WriteVerbose(taskId, String.Format(Resources.CopyDestinationBlobPending, destFile.GetFullPath(), destFile.Share.Name, copyId));
                this.OutputStream.WriteObject(taskId, destFile);
            }
        }
        private async Task StartAsyncCopy(long taskId, CloudFile destFile, Func <bool> checkOverwrite, Func <Task <string> > startCopy)
        {
            bool destExist = true;

            try
            {
                await destFile.FetchAttributesAsync(null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken);
            }
            catch (StorageException ex)
            {
                if (!ex.IsNotFoundException())
                {
                    throw;
                }

                destExist = false;
            }

            if (!destExist || checkOverwrite())
            {
                string copyId = await startCopy();

                this.OutputStream.WriteVerbose(taskId, String.Format(Resources.CopyDestinationBlobPending, destFile.GetFullPath(), destFile.Share.Name, copyId));
                this.OutputStream.WriteObject(taskId, destFile);
            }
        }
Ejemplo n.º 3
0
        public override async Task CopyToLocationAsync(string destination, bool overwrite = false)
        {
            IStorageItem item = destination.CreateItem(Configuration);

            using (Stream source = OpenRead())
            {
                await _file.FetchAttributesAsync();

                using (Stream dest = item.OpenWrite(overwrite, _file.Properties.Length))
                {
                    await source.CopyToAsync(dest);

                    await dest.FlushAsync();
                }
            }
        }
        /// <inheritdoc />
        public async Task <IEnumerable <byte> > DownloadFileAsync(
            string absolutePath,
            CancellationToken cancellationToken)
        {
            byte[] toReturn = null;

            Uri       uri       = new Uri(absolutePath, UriKind.Absolute);
            CloudFile cloudFile = new CloudFile(uri, this.sourceStorageCredentials);

            await cloudFile.FetchAttributesAsync().ConfigureAwait(false);

            toReturn = new byte[cloudFile.Properties.Length];

            this.loggerWrapper.Debug(
                $"Downloading \"{absolutePath}\" ({toReturn.Length} " +
                $"byte(s))...");

            await cloudFile.DownloadToByteArrayAsync(toReturn, 0)
            .ConfigureAwait(false);

            this.loggerWrapper.Info(
                $"Downloaded \"{absolutePath}\" ({toReturn.Length} byte(s)).");

            return(toReturn);
        }
Ejemplo n.º 5
0
        public async Task <IDriveItem> GetItemByFileIdAysnc(string path)
        {
            var file = new CloudFile(new Uri(path), fileShare.ServiceClient.Credentials);

            if (await file.ExistsAsync())
            {
                await file.FetchAttributesAsync();

                return(new DriveFile(file.Uri.ToString(), file.Name, Root, DriveType, file.Properties.Length));
            }

            var dir = new CloudFileDirectory(new Uri(path), fileShare.ServiceClient.Credentials);

            if (await dir.ExistsAsync())
            {
                return(new DriveFolder(dir.Uri.ToString(), dir.Name, Root, DriveType));
            }

            return(null);
            //var split = new Queue<string>(path.Split(new[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries));

            //var folder = this._fileShare.GetRootDirectoryReference();
            //while (split.Count > 1)
            //{
            //    var current = split.Dequeue();
            //    folder = folder.GetDirectoryReference(current);
            //}

            //var name = split.Dequeue();
            //var file = folder.GetFileReference(name);
            //if (file.Exists())
            //{
            //    await file.FetchAttributesAsync();
            //    return new DriveFile(file.Uri.ToString(), file.Name, Root, DriveType, file.Properties.Length);
            //}

            //var dir = folder.GetDirectoryReference(name);
            //if (dir.Exists())
            //{
            //    return new DriveFolder(dir.Uri.ToString(), dir.Name, Root, DriveType);
            //}

            //return null;
        }
Ejemplo n.º 6
0
        protected override async Task <Blob> GetBlobAsync(string fullPath, CancellationToken cancellationToken)
        {
            CloudFile file = await GetFileReferenceAsync(fullPath, false, cancellationToken).ConfigureAwait(false);

            try
            {
                await file.FetchAttributesAsync(cancellationToken).ConfigureAwait(false);

                return(AzConvert.ToBlob(StoragePath.GetParent(fullPath), file));
            }
            catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ShareNotFound")
            {
                return(null);
            }
            catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ResourceNotFound")
            {
                return(null);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads the BLOB storing with the given key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public byte[] ReadBlob(string key)
        {
            CloudFile cloudFile = GetCloudFileFromKey(key);

            try
            {
                if (!cloudFile.ExistsAsync().Result)
                {
                    return(null);
                }
                cloudFile.FetchAttributesAsync().Wait();

                byte[] content = new byte[cloudFile.Properties.Length];
                cloudFile.DownloadToByteArrayAsync(content, 0).Wait();

                return(content);
            }
            catch (Exception ex)
            {
                throw new LendsumException(S.Invariant($"The filename {key} cannot be read in {config.AzureSharedReference} "), ex);
            }
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public byte[] ReadAllBytes(string fileName, string[] path)
        {
            #region validation

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            #endregion

            CloudFileDirectory cloudFileDirectory = CloudFileShare.GetDirectoryReference(path: path);

            CloudFile cloudFile = cloudFileDirectory.GetFileReference(fileName);

            TaskUtilities.ExecuteSync(cloudFile.FetchAttributesAsync());

            var cloudFileBytes = new byte[cloudFile.Properties.Length];

            TaskUtilities.ExecuteSync(cloudFile.DownloadToByteArrayAsync(cloudFileBytes, 0));

            return(cloudFileBytes);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Manage file metadata
        /// </summary>
        /// <param name="cloudFileClient"></param>
        /// <returns></returns>
        private static async Task FileMetadataSample(CloudFileClient cloudFileClient)
        {
            Console.WriteLine();
            // Create the share name -- use a guid in the name so it's unique.
            string         shareName = "demotest-" + Guid.NewGuid();
            CloudFileShare share     = cloudFileClient.GetShareReference(shareName);

            try
            {
                // Create share
                Console.WriteLine("Create Share");
                await share.CreateIfNotExistsAsync();

                // Create directory
                Console.WriteLine("Create directory");
                CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
                await rootDirectory.CreateIfNotExistsAsync();

                CloudFile file = rootDirectory.GetFileReference("demofile");

                // Set file metadata
                Console.WriteLine("Set file metadata");
                file.Metadata.Add("key1", "value1");
                file.Metadata.Add("key2", "value2");

                // Create file
                Console.WriteLine("Create file");
                await file.CreateAsync(1000);

                // Fetch file attributes
                // in this case this call is not need but is included for demo purposes
                await file.FetchAttributesAsync();

                Console.WriteLine("Get file metadata:");
                foreach (var keyValue in file.Metadata)
                {
                    Console.WriteLine("    {0}: {1}", keyValue.Key, keyValue.Value);
                }
                Console.WriteLine();
            }
            catch (StorageException exStorage)
            {
                Common.WriteException(exStorage);
                Console.WriteLine(
                    "Please make sure your storage account is specified correctly in the app.config - then restart the sample.");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine("    Exception thrown creating share.");
                Common.WriteException(ex);
                throw;
            }
            finally
            {
                // Delete share
                Console.WriteLine("Delete share");
                share.DeleteIfExists();
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Manage file properties
        /// </summary>
        /// <param name="cloudFileClient"></param>
        /// <returns></returns>
        private static async Task FilePropertiesSample(CloudFileClient cloudFileClient)
        {
            Console.WriteLine();
            // Create the share name -- use a guid in the name so it's unique.
            string         shareName = "demotest-" + Guid.NewGuid();
            CloudFileShare share     = cloudFileClient.GetShareReference(shareName);

            try
            {
                // Create share
                Console.WriteLine("Create Share");
                await share.CreateIfNotExistsAsync();

                // Create directory
                Console.WriteLine("Create directory");
                CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
                await rootDirectory.CreateIfNotExistsAsync();

                CloudFile file = rootDirectory.GetFileReference("demofile");

                // Set file properties
                file.Properties.ContentType     = "plain/text";
                file.Properties.ContentEncoding = "UTF-8";
                file.Properties.ContentLanguage = "en";

                // Create file
                Console.WriteLine("Create file");
                await file.CreateAsync(1000);

                // Fetch file attributes
                // in this case this call is not need but is included for demo purposes
                await file.FetchAttributesAsync();

                Console.WriteLine("Get file properties:");
                Console.WriteLine("    ETag: {0}", file.Properties.ETag);
                Console.WriteLine("    Content type: {0}", file.Properties.ContentType);
                Console.WriteLine("    Cache control: {0}", file.Properties.CacheControl);
                Console.WriteLine("    Content encoding: {0}", file.Properties.ContentEncoding);
                Console.WriteLine("    Content language: {0}", file.Properties.ContentLanguage);
                Console.WriteLine("    Content disposition: {0}", file.Properties.ContentDisposition);
                Console.WriteLine("    Content MD5: {0}", file.Properties.ContentMD5);
                Console.WriteLine("    Length: {0}", file.Properties.Length);
            }
            catch (StorageException exStorage)
            {
                Common.WriteException(exStorage);
                Console.WriteLine(
                    "Please make sure your storage account is specified correctly in the app.config - then restart the sample.");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine("    Exception thrown creating share.");
                Common.WriteException(ex);
                throw;
            }
            finally
            {
                // Delete share
                Console.WriteLine("Delete share");
                share.DeleteIfExists();
            }
            Console.WriteLine();
        }
Ejemplo n.º 11
0
 public Task FetchFileAttributesAsync(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken token)
 {
     return(file.FetchAttributesAsync(accessCondition, options, operationContext, token));
 }
Ejemplo n.º 12
0
        private static async Task CloudBlockBlobCopyFromCloudFileImpl(Func <CloudFile, CloudBlockBlob, string> copyFunc)
        {
            CloudFileClient fileClient = GenerateCloudFileClient();

            string         name  = GetRandomContainerName();
            CloudFileShare share = fileClient.GetShareReference(name);

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                try
                {
                    await container.CreateAsync();

                    await share.CreateAsync();

                    CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");
                    byte[]    data   = GetRandomBuffer(1024);

                    await source.UploadFromByteArrayAsync(data, 0, data.Length);

                    source.Metadata["Test"] = "value";
                    source.SetMetadataAsync().Wait();

                    var sasToken = source.GetSharedAccessSignature(new SharedAccessFilePolicy
                    {
                        Permissions            = SharedAccessFilePermissions.Read,
                        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
                    });

                    Uri fileSasUri = new Uri(source.StorageUri.PrimaryUri.ToString() + sasToken);

                    source = new CloudFile(fileSasUri);

                    CloudBlockBlob copy   = container.GetBlockBlobReference("copy");
                    string         copyId = copyFunc(source, copy);
                    Assert.AreEqual(BlobType.BlockBlob, copy.BlobType);

                    try
                    {
                        await WaitForCopyAsync(copy);

                        Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                        Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                        Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                        Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                        Assert.AreEqual(copyId, copy.CopyState.CopyId);
                        Assert.IsFalse(copy.Properties.IsIncrementalCopy);
                        Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));
                    }
                    catch (NullReferenceException)
                    {
                        // potential null ref in the WaitForCopyTask and CopyState check implementation
                    }

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await copy.AbortCopyAsync(copyId, null, null, opContext),
                        opContext,
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");

                    source.FetchAttributesAsync().Wait();
                    Assert.IsNotNull(copy.Properties.ETag);
                    Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag);
                    Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                    byte[] copyData = new byte[source.Properties.Length];

                    await copy.DownloadToByteArrayAsync(copyData, 0);

                    Assert.IsTrue(data.SequenceEqual(copyData), "Data inside copy of blob not similar");

                    copy.FetchAttributesAsync().Wait();
                    BlobProperties prop1 = copy.Properties;
                    FileProperties prop2 = source.Properties;

                    Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                    Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                    Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                    Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                    Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                    Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                    copy.DeleteIfExistsAsync().Wait();
                }
                finally
                {
                    share.DeleteIfExistsAsync().Wait();
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
 public static void FetchAttributes(this CloudFile file, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     file.FetchAttributesAsync(accessCondition, options, operationContext).GetAwaiter().GetResult();
 }