public Uri UploadFileToBlob(BlobUploadParameters parameters)
 {
     StorageAccountGetKeysResponse keys = StorageManagementClient.StorageAccounts.GetKeys(parameters.StorageName);
     string storageKey = keys.PrimaryKey;
     StorageAccountGetResponse storageService = StorageManagementClient.StorageAccounts.Get(parameters.StorageName);
     Uri blobEndpointUri = storageService.StorageAccount.Properties.Endpoints[0];
     return UploadFile(parameters.StorageName,
         GeneralUtilities.CreateHttpsEndpoint(blobEndpointUri.ToString()),
         storageKey, parameters);
 }
Ejemplo n.º 2
0
        public Uri UploadFileToBlob(BlobUploadParameters parameters)
        {
            StorageAccountGetKeysResponse keys = StorageManagementClient.StorageAccounts.GetKeys(parameters.StorageName);
            string storageKey = keys.PrimaryKey;
            StorageAccountGetResponse storageService = StorageManagementClient.StorageAccounts.Get(parameters.StorageName);
            Uri blobEndpointUri = storageService.StorageAccount.Properties.Endpoints[0];

            return(UploadFile(parameters.StorageName,
                              GeneralUtilities.CreateHttpsEndpoint(blobEndpointUri.ToString()),
                              storageKey, parameters));
        }
Ejemplo n.º 3
0
        private Uri UploadFile(string storageName, Uri blobEndpointUri, string storageKey, BlobUploadParameters parameters)
        {
            var    credentials = new StorageCredentials(storageName, storageKey);
            var    client      = new CloudBlobClient(blobEndpointUri, credentials);
            string blobName    = parameters.FileRemoteName;

            if (string.IsNullOrEmpty(blobName))
            {
                blobName = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}_{1}",
                    DateTime.UtcNow.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture),
                    Path.GetFileName(parameters.FileLocalPath));
            }

            CloudBlobContainer container = client.GetContainerReference(parameters.ContainerName);
            var wasCreated = container.CreateIfNotExists();

            if (wasCreated && parameters.ContainerPublic)
            {
                container.SetPermissions(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
            }

            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            if (blob.Exists())
            {
                if (parameters.OverrideIfExists)
                {
                    blob.DeleteIfExists();
                }
                else
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              Commands.Common.Properties.Resources.BlobAlreadyExistsInTheAccount, blobName));
                }
            }

            using (FileStream readStream = File.OpenRead(parameters.FileLocalPath))
            {
                blob.UploadFromStream(readStream, AccessCondition.GenerateEmptyCondition(), parameters.BlobRequestOptions);
            }

            blob = container.GetBlockBlobReference(blobName);

            string sasContainerToken = string.Empty;

            if (!parameters.ContainerPublic)
            {
                //Set the expiry time and permissions for the blob.
                //Start time is specified as a few minutes in the past, to mitigate clock skew.
                //The shared access signature will be valid immediately.
                SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
                sasConstraints.SharedAccessStartTime  = DateTime.UtcNow.AddMinutes(-5);
                sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(parameters.SasTokenDurationInHours);
                sasConstraints.Permissions            = SharedAccessBlobPermissions.Read;

                //Generate the shared access signature on the blob, setting the constraints directly on the signature.
                sasContainerToken = blob.GetSharedAccessSignature(sasConstraints);
            }

            string fullUrl = client.BaseUri + parameters.ContainerName + client.DefaultDelimiter + blobName + sasContainerToken;

            return(new Uri(fullUrl));
        }
        private Uri UploadFile(string storageName, Uri blobEndpointUri, string storageKey, BlobUploadParameters parameters)
        {
            var credentials = new StorageCredentials(storageName, storageKey);
            var client = new CloudBlobClient(blobEndpointUri, credentials);
            string blobName = parameters.FileRemoteName;
            if (string.IsNullOrEmpty(blobName))
            {
                blobName = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}_{1}",
                    DateTime.UtcNow.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture),
                    Path.GetFileName(parameters.FileLocalPath));
            }

            CloudBlobContainer container = client.GetContainerReference(parameters.ContainerName);
            var wasCreated = container.CreateIfNotExists();
            if (wasCreated && parameters.ContainerPublic)
            {
                container.SetPermissions(new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    });
            }

            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            if (blob.Exists())
            {
                if (parameters.OverrideIfExists)
                {
                    blob.DeleteIfExists();
                }
                else
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                        Commands.Common.Properties.Resources.BlobAlreadyExistsInTheAccount, blobName));
                }
            }

            using (FileStream readStream = File.OpenRead(parameters.FileLocalPath))
            {
                blob.UploadFromStream(readStream, AccessCondition.GenerateEmptyCondition(), parameters.BlobRequestOptions);
            }

            blob = container.GetBlockBlobReference(blobName);

            string sasContainerToken = string.Empty;

            if (!parameters.ContainerPublic)
            {
                //Set the expiry time and permissions for the blob.
                //Start time is specified as a few minutes in the past, to mitigate clock skew.
                //The shared access signature will be valid immediately.
                SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
                sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
                sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(parameters.SasTokenDurationInHours);
                sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

                //Generate the shared access signature on the blob, setting the constraints directly on the signature.
                sasContainerToken = blob.GetSharedAccessSignature(sasConstraints);
            }

            string fullUrl = client.BaseUri + parameters.ContainerName + client.DefaultDelimiter + blobName + sasContainerToken;

            return new Uri(fullUrl);
        }