Esempio n. 1
0
        public async Task WriteFileAsync(string share, string filename, Stream source, string contentType = "application/octet-stream", CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(share))
            {
                throw new ArgumentNullException("share");
            }

            if (string.IsNullOrEmpty("filename"))
            {
                throw new ArgumentNullException("filename");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;

            IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                progress =>
            {
                bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                {
                    OnUploadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(share, filename, bytesTransferred, source.Length));
                }
            });

            try
            {
                CloudFileShare     choudShare = client.GetShareReference(share);
                CloudFileDirectory dir        = choudShare.GetRootDirectoryReference();
                CloudFile          file       = dir.GetFileReference(filename);
                file.Properties.ContentType = contentType;
                await file.UploadFromStreamAsync(source, source.Length, default(AccessCondition), default(FileRequestOptions), default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                if (ex.InnerException is TaskCanceledException)
                {
                    source = null;
                }
                else
                {
                    error = ex;
                    throw ex;
                }
            }
            finally
            {
                watch.Stop();
                OnUploadCompleted?.Invoke(this, new BlobCompleteEventArgs(share, filename, token.IsCancellationRequested, error));
            }
        }
Esempio n. 2
0
        public async Task WriteBlockBlobAsync(string containerName, string filename, Stream source,
                                              string contentType = "application/octet-stream", CancellationToken token = default)
        {
            _ = filename ?? throw new ArgumentNullException(nameof(filename));
            _ = source ?? throw new ArgumentNullException(nameof(source));

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;

            IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                progress =>
            {
                bytesTransferred = bytesTransferred < progress.BytesTransferred
                        ? progress.BytesTransferred
                        : bytesTransferred;
                if (watch.Elapsed.TotalMilliseconds > time + 1000.0 &&
                    bytesTransferred <= progress.BytesTransferred)
                {
                    OnUploadBytesTransferred?.Invoke(this,
                                                     new BytesTransferredEventArgs(containerName, filename, bytesTransferred, source.Length));
                }
            });

            try
            {
                CloudBlobContainer container = await GetContainerReferenceAsync(containerName);

                CloudBlockBlob blob = container.GetBlockBlobReference(filename);
                blob.Properties.ContentType = contentType;

                await blob.UploadFromStreamAsync(source, default, default, default, progressHandler, token);
Esempio n. 3
0
        public async Task WritePageBlobAsync(string containerName, string filename, byte[] source, string contentType = "application/octet-stream", CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if (source == null)
            {
                throw new ArgumentException("source");
            }

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;

            IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                progress =>
            {
                bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                {
                    OnUploadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(containerName, filename, bytesTransferred, source.Length));
                }
            });

            try
            {
                CloudBlobContainer container = await GetContainerReferenceAsync(containerName);

                CloudPageBlob blob = container.GetPageBlobReference(filename);

                blob.Properties.ContentType = contentType;
                await blob.UploadFromByteArrayAsync(source, 0, source.Length, null, default(AccessCondition), default(BlobRequestOptions), default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                if (ex.InnerException is TaskCanceledException)
                {
                    source = null;
                }
                else
                {
                    error = ex;
                    throw ex;
                }
            }
            finally
            {
                watch.Stop();
                OnUploadCompleted?.Invoke(this, new BlobCompleteEventArgs(containerName, filename, token.IsCancellationRequested, error));
            }
        }
Esempio n. 4
0
        private async Task UploadAsync(CloudBlockBlob blob, byte[] buffer, CancellationToken token)
        {
            string fullpath = blob.Container.Name + "/" + blob.Name;

            Stopwatch watch = new Stopwatch();

            watch.Start();
            double             time             = watch.Elapsed.TotalMilliseconds;
            long               bytesTransferred = 0;
            BlobRequestOptions options          = new BlobRequestOptions();

            options.SingleBlobUploadThresholdInBytes = 1048576;


            IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                progress =>
            {
                bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                {
                    OnUploadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(fullpath, buffer.Length, progress.BytesTransferred));
                }
            });

            try
            {
                await blob.UploadFromByteArrayAsync(buffer, 0, buffer.Length, default(AccessCondition), options, default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                watch.Stop();
                if (ex.InnerException is TaskCanceledException)
                {
                    buffer = null;
                }
                else
                {
                    throw ex;
                }
            }
            OnUploadCompleted?.Invoke(this, new BlobCompleteEventArgs(fullpath, token.IsCancellationRequested));
        }
 private void Remote_OnUploadBytesTransferred(object sender, BytesTransferredEventArgs e)
 {
     OnUploadBytesTransferred?.Invoke(this, e);
 }
Esempio n. 6
0
        public async Task UploadFileToBlockBlob(string filePath, string containerName, string blobFilename, string contentType = "application/octet-stream", CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filenamePath");
            }

            if (string.IsNullOrEmpty(containerName))
            {
                throw new ArgumentNullException("containerName");
            }

            if (string.IsNullOrEmpty("blobFilename"))
            {
                throw new ArgumentNullException("blobFilename");
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(filePath);
            }

            Exception error = null;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            double time             = watch.Elapsed.TotalMilliseconds;
            long   bytesTransferred = 0;

            IProgress <StorageProgress> progressHandler = new Progress <StorageProgress>(
                progress =>
            {
                bytesTransferred = bytesTransferred < progress.BytesTransferred ? progress.BytesTransferred : bytesTransferred;
                FileInfo info    = new FileInfo(filePath);
                if (watch.Elapsed.TotalMilliseconds > time + 1000.0 && bytesTransferred <= progress.BytesTransferred)
                {
                    OnUploadBytesTransferred?.Invoke(this, new BytesTransferredEventArgs(containerName, blobFilename, bytesTransferred, info.Length));
                }
            });

            try
            {
                CloudBlobContainer container = await GetContainerReferenceAsync(containerName);

                CloudBlockBlob blob = container.GetBlockBlobReference(blobFilename);
                await blob.UploadFromFileAsync(filePath, default(AccessCondition), default(BlobRequestOptions), default(OperationContext), progressHandler, token);
            }
            catch (Exception ex)
            {
                if (!(ex.InnerException is TaskCanceledException))
                {
                    error = ex;
                    throw ex;
                }
            }
            finally
            {
                watch.Stop();
                OnUploadCompleted?.Invoke(this, new BlobCompleteEventArgs(containerName, blobFilename, token.IsCancellationRequested, error));
            }
        }