Ejemplo n.º 1
0
        public async void Upload(UploadMethod uploadMethod, int megabytes, bool useAsync, bool useNonAsciiFilename)
        {
            var shareFileClient = GetShareFileClient();
            var rootFolder = shareFileClient.Items.Get().Execute();
            var testFolder = new Folder { Name = RandomString(30) + ".txt" };

            testFolder = shareFileClient.Items.CreateFolder(rootFolder.url, testFolder).Execute();
            var file = GetFileToUpload(1024 * 1024 * megabytes, useNonAsciiFilename);
            var uploadSpec = new UploadSpecificationRequest(file.Name, file.Length, testFolder.url, uploadMethod);

            UploaderBase uploader;

            if (useAsync)
            {
                uploader = shareFileClient.GetAsyncFileUploader(uploadSpec, file);
            }
            else
            {
                uploader = shareFileClient.GetFileUploader(uploadSpec, file);
            }

            var progressInvocations = 0;
            var bytesTransferred = 0L;
            uploader.OnTransferProgress += (sender, args) =>
            {
                bytesTransferred = args.Progress.BytesTransferred;
                progressInvocations++;
            };

            UploadResponse uploadResponse;

            if (useAsync)
            {
                uploadResponse = await ((AsyncUploaderBase)uploader).UploadAsync();
            }
            else
            {
                uploadResponse = ((SyncUploaderBase)uploader).Upload();
            }

            shareFileClient.Items.Delete(testFolder.url);

            uploadResponse.FirstOrDefault().Should().NotBeNull();
            var expectedInvocations = Math.Ceiling((double)file.Length / UploaderBase.DefaultBufferLength) + 1;

            bytesTransferred.Should().Be(1024 * 1024 * megabytes);

            if (uploadMethod == UploadMethod.Standard)
            {
                progressInvocations.Should().Be((int)expectedInvocations, "Standard should be predictable for number of progress callbacks");
            }
            else if (uploadMethod == UploadMethod.Threaded)
            {
                progressInvocations.Should()
                    .BeLessOrEqualTo(
                        (int)expectedInvocations,
                        "Threaded scales, therefore byte ranges vary and are less predictable.  We should see no more expectedInvoations");
            }
        }
Ejemplo n.º 2
0
        public static async Task<string> Upload(ShareFileClient sfClient, Folder destinationFolder)
        {
            var file = System.IO.File.Open("SampleFileUpload.txt", FileMode.OpenOrCreate);
            var uploadRequest = new UploadSpecificationRequest
            {
                FileName = "SampleFileUpload.txt",
                FileSize = file.Length,
                Details = "Sample details",
                Parent = destinationFolder.url
            };

            var uploader = sfClient.GetAsyncFileUploader(uploadRequest,
                new PlatformFileStream(file, file.Length, "SampleFileUpload.txt"));

            var uploadResponse = await uploader.UploadAsync();

            return uploadResponse.First().Id;
        }
Ejemplo n.º 3
0
        public Type VerifyUploader(int megabytes, bool useAsync, CapabilityName[] capabilityNames)
        {
            var shareFileClient = GetShareFileClient();
            var testFolder = new Folder { Name = RandomString(30) + ".txt" };
            var file = GetFileToUpload(1024 * 1024 * megabytes, false);
            var uploadSpec = new UploadSpecificationRequest(file.Name, file.Length, testFolder.url);
            if (capabilityNames != null)
            {
                uploadSpec.ProviderCapabilities =
                    new List<Capability>(capabilityNames.Select(x => new Capability { Name = x }));
            }

            UploaderBase uploader;

            if (useAsync)
            {
                uploader = shareFileClient.GetAsyncFileUploader(uploadSpec, file);
            }
            else
            {
                uploader = shareFileClient.GetFileUploader(uploadSpec, file);
            }

            return uploader.GetType();
        }