Esempio n. 1
1
 static string GetContainerSasUri(CloudBlobContainer container)
 {
     SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
     sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24);
     sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List;
     string sasContainerToken = container.GetSharedAccessSignature(sasConstraints);
     return container.Uri + sasContainerToken;
 }
Esempio n. 2
1
 public static string GetReadAccessSignature(CloudBlobContainer container)
 {
     var signature = container.GetSharedAccessSignature(new SharedAccessBlobPolicy {
         Permissions = SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Read,
         SharedAccessExpiryTime = DateTimeOffset.Now.AddDays(7),
     });
     return container.Uri + signature;
 }
Esempio n. 3
0
        public string GetSharedAccessSignature(CloudSharedAccessBlobPolicy sharedAccessBlobPolicy)
        {
            var sasContainerToken =
                m_cloudBlobContainer.GetSharedAccessSignature(sharedAccessBlobPolicy.m_sharedAccessBlobPolicy);

            return(sasContainerToken);
        }
Esempio n. 4
0
        //private CloudBlobClient blobclient;
        /// <summary>
        /// Get Container's SaS Uri
        /// </summary>
        /// <param name="blobclient"></param>
        /// <param name="container"></param>
        /// <param name="permissionType">READ,WRITE,LIST OR DELETE e.g. RWLD</param>
        /// <returns>Uri for container</returns>
        public string GetContainerSasUri(CloudBlobContainer container, string permissionType)
        {
            SharedAccessBlobPolicy sasConstraints = CreateSASPermission(permissionType);

            //Generate the shared access signature on the container. In this case, all of the constraints for the
            //shared access signature are specified on the stored access policy.
            string sasContainerToken = container.GetSharedAccessSignature(sasConstraints);

            //Return the URI string for the container, including the SAS token.
            return container.Uri + sasContainerToken;
        }
Esempio n. 5
0
        static string GetContainerSasUri(CloudBlobContainer container)
        {
            //Set the expiry time and permissions for the container.
            //In this case no start time is specified, so the shared access signature becomes valid immediately.
            SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
            sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24);
            sasConstraints.SharedAccessStartTime = DateTime.UtcNow;
            sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List;

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

            //Return the URI string for the container, including the SAS token.
            return container.Uri + sasContainerToken;
        }
        public async Task CloudBlobContainerWebContainerOperationsAsync()
        {
            // Test operations with shard key
            CloudBlobClient    blobClient   = GenerateCloudBlobClient();
            CloudBlobContainer webContainer = blobClient.GetContainerReference("$web");

            try
            {
                await webContainer.DeleteIfExistsAsync();

                Assert.IsFalse(await webContainer.ExistsAsync());
                await TestHelper.SpinUpToNSecondsIgnoringFailuresAsync(async() => await webContainer.CreateAsync(), 120);

                Assert.IsTrue(await webContainer.ExistsAsync());
                Assert.IsTrue(
                    (await blobClient.ListContainersSegmentedAsync("$", null))
                    .Results
                    .Any(container => container.Name == webContainer.Name)
                    );

                await ValidateWebContainerAsync(webContainer);

                // Clear out the old data, faster than deleting / re-creating the container.
                foreach (CloudBlob blob in (await webContainer.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.All, default(int?), default(BlobContinuationToken), default(BlobRequestOptions), default(OperationContext))).Results)
                {
                    await blob.DeleteAsync();
                }

                // Test relevant operations with a service SAS.
                string webContainerSAS = webContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy()
                {
                    SharedAccessExpiryTime = DateTime.Now + TimeSpan.FromDays(30), Permissions = SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Write
                });
                await ValidateWebContainerAsync(new CloudBlobContainer(new Uri(webContainer.Uri + webContainerSAS)));

                await webContainer.DeleteAsync();

                Assert.IsFalse(
                    (await blobClient.ListContainersSegmentedAsync("$", null))
                    .Results
                    .Any(container => container.Name == webContainer.Name)
                    );
            }
            finally
            {
                webContainer.DeleteIfExistsAsync().Wait();
            }
        }
        //TODO: move to workerEncoder
        private IAsset CreateAssetFromBlob(CloudBlobContainer externalMediaBlobContainer, string ExternalBlobName, CloudBlobClient assetBlobClient, string MediaServicesBlobName, string myProcessId)
        {
            // Create a new asset.
            //myProcessId = Guid.NewGuid().ToString();

            CloudMediaContext MediaContext = ObtainContext(_accountMediaName, _accountMediaKey);
            string assetName = string.Format("{0}_{1}_Butler_{2}", externalMediaBlobContainer.Name, ExternalBlobName, myProcessId);
            IAsset asset = MediaContext.Assets.Create(assetName, AssetCreationOptions.None);
            IAccessPolicy writePolicy=MediaContext.AccessPolicies.Create("writePolicy_" + assetName, TimeSpan.FromMinutes(120), AccessPermissions.Write);
            ILocator destinationLocator=MediaContext.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy);

            string assetContainerName = (new Uri(destinationLocator.Path)).Segments[1];
            CloudBlobContainer assetContainer = assetBlobClient.GetContainerReference(assetContainerName);
            CloudBlockBlob ExternalBlob = externalMediaBlobContainer.GetBlockBlobReference(ExternalBlobName);
            CloudBlockBlob assetBlob = assetContainer.GetBlockBlobReference(MediaServicesBlobName);

            var sas = externalMediaBlobContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
                SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),
                Permissions = SharedAccessBlobPermissions.Read,
            });
            var srcBlockBlobSasUri = string.Format("{0}{1}", ExternalBlob.Uri, sas);
            assetBlob.StartCopyFromBlob(new Uri(srcBlockBlobSasUri));

            CloudBlockBlob blobStatusCheck;
            blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(MediaServicesBlobName);
            while (blobStatusCheck.CopyState.Status == CopyStatus.Pending)
            {
                Task.Delay(TimeSpan.FromSeconds(10d)).Wait();
                Trace.TraceInformation("Waiting copy of  " + blobStatusCheck.Name);
                blobStatusCheck = (CloudBlockBlob)assetContainer.GetBlobReferenceFromServer(MediaServicesBlobName);
            }
            assetBlob.FetchAttributes();

            var assetFile = asset.AssetFiles.Create(MediaServicesBlobName);
            destinationLocator.Delete();
            writePolicy.Delete();
            //// Refresh the asset.
            asset = MediaContext.Assets.Where(a => a.Id == asset.Id).FirstOrDefault();
            return asset;
        }
        /// <summary>
        /// Copy Blob from Origin to Target Container
        /// </summary>
        /// <param name="OriginContainer"></param>
        /// <param name="OriginBlobURL"></param>
        /// <param name="DestContainer"></param>
        /// <param name="DestBlobURL"></param>
        private void startCopyBlob(CloudBlobContainer OriginContainer, string OriginBlobURL, CloudBlobContainer DestContainer, string DestBlobURL)
        {
            //Destination
            Uri destinUri = new Uri(DestBlobURL);
            string destinBlobName = Uri.UnescapeDataString(destinUri.Segments[destinUri.Segments.Count() - 1]);
            CloudBlockBlob destinBlockBloc = DestContainer.GetBlockBlobReference(destinBlobName);
            DestContainer.SetPermissions(new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    }
               );

            //Origin
            Uri originUri = new Uri(OriginBlobURL);
            string originBlobName = Uri.UnescapeDataString(originUri.Segments[originUri.Segments.Count() - 1]);
            CloudBlockBlob originBlockBlob = OriginContainer.GetBlockBlobReference(originBlobName);
            var sas = OriginContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
                SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),
                Permissions = SharedAccessBlobPermissions.Read,
            });

            var srcBlockBlobSasUri = string.Format("{0}{1}", Uri.UnescapeDataString(originBlockBlob.Uri.AbsoluteUri), sas);
            //Start Copy
            destinBlockBloc.StartCopyFromBlob(new Uri(srcBlockBlobSasUri));
        }
Esempio n. 9
0
 private static string BuildSAS(CloudBlobContainer container)
 {
     var sas = container.GetSharedAccessSignature(new SharedAccessBlobPolicy()
     {
         SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5),
         SharedAccessExpiryTime = DateTime.UtcNow.AddYears(2),
         Permissions =
             SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Delete |
             SharedAccessBlobPermissions.List
     });
     return sas;
 }
Esempio n. 10
0
        public static void CopyBlobs(
                CloudBlobContainer srcContainer,
                string policyId,
                CloudBlobContainer destContainer)
        {
            // get the SAS token to use for all blobs
            string blobToken = srcContainer.GetSharedAccessSignature(
                               new SharedAccessBlobPolicy(), policyId);


            var srcBlobList = srcContainer.ListBlobs(null, false);
            foreach (var src in srcBlobList)
            {
                var srcBlob = src as CloudBlob;

                try {

                    // Create appropriate destination blob type to match the source blob
                    CloudBlob destBlob;
                    if (srcBlob.Properties.BlobType == BlobType.BlockBlob)
                    {
                        destBlob = destContainer.GetBlockBlobReference(srcBlob.Name);
                    }
                    else
                    {
                        destBlob = destContainer.GetPageBlobReference(srcBlob.Name);
                    }

                    // copy using src blob as SAS
                    // destBlob.StartCopyFromBlob(new Uri(srcBlob.Uri.AbsoluteUri + blobToken));

                    destBlob.StartCopy(new Uri(srcBlob.Uri.AbsoluteUri));
                    Console.WriteLine("Copied: " + srcBlob.Uri.AbsoluteUri);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: "  + e.Message);
                }
            }
        }
        /// <summary>
        /// Gets the container sas URI.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="expireOffsetInMinute">The expire offset in minute.</param>
        /// <param name="permission">The permission.</param>
        /// <returns>System.String.</returns>
        protected static string GenerateContainerUri(CloudBlobContainer container, int expireOffsetInMinute = 10, SharedAccessBlobPermissions permission = SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Read)
        {
            //Set the expiry time and permissions for the container.
            //In this case no start time is specified, so the shared access signature becomes valid immediately.
            var sasConstraints = new SharedAccessBlobPolicy();
            sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(expireOffsetInMinute);
            sasConstraints.Permissions = permission;

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

            //Return the URI string for the container, including the SAS token.
            return container.Uri + sasContainerToken;
        }
Esempio n. 12
0
 //calculates SAS string to have access to a container
 public static string CalculateSASStringForContainer(string method, CloudBlobContainer container)
 {
     SharedAccessBlobPolicy sasConstraints = GetSasPolicy(method);
     //Generate the shared access signature on the container, setting the constraints directly on the signature.
     return container.GetSharedAccessSignature(sasConstraints);
 }
Esempio n. 13
-1
        static string GetContainerSasUriWithPolicy(CloudBlobContainer container, string policyName)
        {
            //Generate the shared access signature on the container. In this case, all of the constraints for the 
            //shared access signature are specified on the stored access policy.
            string sasContainerToken = container.GetSharedAccessSignature(null, policyName);

            //Return the URI string for the container, including the SAS token.
            return container.Uri + sasContainerToken;
        }
        /// <summary>
        /// Returns a URI containing a SAS for the blob container.
        /// </summary>
        /// <param name="container">A reference to the container.</param>
        /// <param name="storedPolicyName">A string containing the name of the stored access policy. If null, an ad-hoc SAS is created.</param>
        /// <returns>A string containing the URI for the container, with the SAS token appended.</returns>
        private static string GetContainerSasUri(CloudBlobContainer container, string storedPolicyName = null)
        {
            string sasContainerToken;

            // If no stored policy is specified, create a new access policy and define its constraints.
            if (storedPolicyName == null)
            {
                // Note that the SharedAccessBlobPolicy class is used both to define the parameters of an ad-hoc SAS, and 
                // to construct a shared access policy that is saved to the container's shared access policies. 
                SharedAccessBlobPolicy adHocPolicy = new SharedAccessBlobPolicy()
                {
                    // When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request. 
                    // Omitting the start time for a SAS that is effective immediately helps to avoid clock skew.
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
                    Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List
                };

                // Generate the shared access signature on the container, setting the constraints directly on the signature.
                sasContainerToken = container.GetSharedAccessSignature(adHocPolicy, null);

                Console.WriteLine("SAS for blob container (ad hoc): {0}", sasContainerToken);
                Console.WriteLine();
            }
            else
            {
                // Generate the shared access signature on the container. In this case, all of the constraints for the
                // shared access signature are specified on the stored access policy, which is provided by name.
                // It is also possible to specify some constraints on an ad-hoc SAS and others on the stored access policy.
                sasContainerToken = container.GetSharedAccessSignature(null, storedPolicyName);

                Console.WriteLine("SAS for blob container (stored access policy): {0}", sasContainerToken);
                Console.WriteLine();
            }

            // Return the URI string for the container, including the SAS token.
            return container.Uri + sasContainerToken;
        }
Esempio n. 15
-1
        static string GetContainerSasUriWithPolicy(CloudBlobContainer container, string policyName)
        {
            string sasContainerToken = container.GetSharedAccessSignature(null, policyName);
            return container.Uri + sasContainerToken;

        }