Ejemplo n.º 1
0
        public static async Task <IEnumerable <IListFileItem> > ListFilesAndDirectoriesAsync(CloudFileDirectory directory, string prefix, int?maxResults, FileRequestOptions options, OperationContext operationContext)
        {
            List <IListFileItem>  results = new List <IListFileItem>();
            FileContinuationToken token   = null;

            do
            {
                FileResultSegment resultSegment = await directory.ListFilesAndDirectoriesSegmentedAsync(prefix, maxResults, token, options, operationContext);

                results.AddRange(resultSegment.Results);
                token = resultSegment.ContinuationToken;
            }while (token != null);
            return(results);
        }
Ejemplo n.º 2
0
        private static async Task TestAccess(string sasToken, SharedAccessFilePermissions permissions, SharedAccessFileHeaders headers, CloudFileShare share, CloudFile file)
        {
            CloudFileShare     SASshare = null;
            CloudFile          SASfile;
            OperationContext   context     = new OperationContext();
            StorageCredentials credentials = string.IsNullOrEmpty(sasToken) ?
                                             new StorageCredentials() :
                                             new StorageCredentials(sasToken);
            string fileText = "file";

            if (share != null)
            {
                SASshare = new CloudFileShare(credentials.TransformUri(share.Uri));
                SASfile  = SASshare.GetRootDirectoryReference().GetFileReference(file.Name);
            }
            else
            {
                SASfile = new CloudFile(credentials.TransformUri(file.Uri));
            }

            if ((permissions & SharedAccessFilePermissions.Write) == SharedAccessFilePermissions.Write)
            {
                await UploadTextAsync(SASfile, fileText, Encoding.UTF8);
            }
            else if ((permissions & SharedAccessFilePermissions.Create) == SharedAccessFilePermissions.Create)
            {
                await SASfile.CreateAsync(Encoding.UTF8.GetBytes(fileText).Length);

                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await UploadTextAsync(SASfile, fileText, Encoding.UTF8, operationContext: context),
                    context,
                    "UploadText SAS does not allow for writing",
                    HttpStatusCode.Forbidden,
                    "");
                await UploadTextAsync(file, fileText, Encoding.UTF8);
            }
            else
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.CreateAsync(Encoding.UTF8.GetBytes(fileText).Length, null /* accessCondition */, null /* options */, context),
                    context,
                    "Create file succeeded but SAS does not allow for writing/creating",
                    HttpStatusCode.Forbidden,
                    "");

                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await UploadTextAsync(SASfile, fileText, Encoding.UTF8, operationContext: context),
                    context,
                    "UploadText SAS does not allow for writing/creating",
                    HttpStatusCode.Forbidden,
                    "");
                await UploadTextAsync(file, fileText, Encoding.UTF8);
            }

            if (SASshare != null)
            {
                if ((permissions & SharedAccessFilePermissions.List) == SharedAccessFilePermissions.List)
                {
                    FileResultSegment results = await SASshare.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(null);
                }
                else
                {
                    context = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => { FileResultSegment results = await SASshare.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(null /* maxResults */, null /* currentToken */, null /* options */, context); },
                        context,
                        "List files while SAS does not allow for listing",
                        HttpStatusCode.Forbidden,
                        "");
                }
            }

            if ((permissions & SharedAccessFilePermissions.Read) == SharedAccessFilePermissions.Read)
            {
                await SASfile.FetchAttributesAsync();

                // Test headers
                if (headers != null)
                {
                    if (headers.CacheControl != null)
                    {
                        Assert.AreEqual(headers.CacheControl, SASfile.Properties.CacheControl);
                    }

                    if (headers.ContentDisposition != null)
                    {
                        Assert.AreEqual(headers.ContentDisposition, SASfile.Properties.ContentDisposition);
                    }

                    if (headers.ContentEncoding != null)
                    {
                        Assert.AreEqual(headers.ContentEncoding, SASfile.Properties.ContentEncoding);
                    }

                    if (headers.ContentLanguage != null)
                    {
                        Assert.AreEqual(headers.ContentLanguage, SASfile.Properties.ContentLanguage);
                    }

                    if (headers.ContentType != null)
                    {
                        Assert.AreEqual(headers.ContentType, SASfile.Properties.ContentType);
                    }
                }
            }
            else
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.FetchAttributesAsync(null /* accessCondition */, null /* options */, context),
                    context,
                    "Fetch file attributes while SAS does not allow for reading",
                    HttpStatusCode.Forbidden,
                    "");
            }

            if ((permissions & SharedAccessFilePermissions.Write) == SharedAccessFilePermissions.Write)
            {
                await SASfile.SetMetadataAsync();
            }
            else
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.SetMetadataAsync(null /* accessCondition */, null /* options */, context),
                    context,
                    "Set file metadata while SAS does not allow for writing",
                    HttpStatusCode.Forbidden,
                    "");
            }

            if ((permissions & SharedAccessFilePermissions.Delete) == SharedAccessFilePermissions.Delete)
            {
                await SASfile.DeleteAsync();
            }
            else
            {
                context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await SASfile.DeleteAsync(null /* accessCondition */, null /* options */, context),
                    context,
                    "Delete file while SAS does not allow for deleting",
                    HttpStatusCode.Forbidden,
                    "");
            }
        }