Exemplo n.º 1
0
        /// <summary>
        /// Wrapped function of downloading partial file with timeout
        /// </summary>
        /// <param name="filename">The file name to download to.</param>
        /// <param name="offset">Start offset of the download.</param>
        /// <param name="length">The lenth of content to download.</param>
        /// <param name="timeout">The timeout for performing the download operation.</param>
        public void DownloadPartToFile(string filename, int offset, int length, TimeSpan timeout)
        {
            UpDownloadParams downloadParam = new UpDownloadParams(this.blob, filename, offset, length) as UpDownloadParams;
            int caluatedTimeout            = GetTimeOutInSeconds(downloadParam.Length);

            this.reqOption.MaximumExecutionTime = (timeout == TimeSpan.MaxValue || timeout.Seconds < caluatedTimeout)
                ? new TimeSpan(0, 0, caluatedTimeout)
                : timeout;
            using (FileStream fs = FabricFile.Open(downloadParam.Filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                // Create the Blob and upload the file
#if !DotNetCoreClr
                using (Stream blobStream = downloadParam.Blob.OpenRead(null, this.reqOption))
#else
                using (Stream blobStream = downloadParam.Blob.OpenReadAsync(null, this.reqOption, null).Result)
#endif
                {
                    int    len    = 0;
                    byte[] buffer = new byte[PartialDownloadBufferLen];
                    while (len < downloadParam.Length)
                    {
                        blobStream.Position = downloadParam.Offset + len;
                        int count = blobStream.Read(buffer, 0, Math.Min(PartialDownloadBufferLen, downloadParam.Length - len));
                        fs.Write(buffer, 0, count);
                        len += count;
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Wrapped function of downloading file with timeout
        /// </summary>
        /// <param name="filename">The filename to which to download.</param>
        /// <param name="length">The length of the content to be downloaded</param>
        /// <param name="timeout">The timeout for performing the download operation.</param>
        /// <param name="operContext">Represents the context for a request operation.</param>
        public void DownloadToFile(string filename, int length, TimeSpan timeout, OperationContext operContext)
        {
            UpDownloadParams downloadParam = new UpDownloadParams(this.blob, filename, 0, length);
            int caluatedTimeout            = GetTimeOutInSeconds(downloadParam.Length);

            this.reqOption.MaximumExecutionTime = (timeout == TimeSpan.MaxValue || timeout.Seconds < caluatedTimeout)
                ? new TimeSpan(0, 0, caluatedTimeout)
                : timeout;

#if !DotNetCoreClr
            downloadParam.Blob.DownloadToFile(downloadParam.Filename, FileMode.CreateNew, null, this.reqOption, operContext);
#else
            downloadParam.Blob.DownloadToFileAsync(downloadParam.Filename, FileMode.CreateNew, null, this.reqOption, operContext).Wait();
#endif
        }
Exemplo n.º 3
0
        /// <summary>
        /// Wrapped function of uploading file with timeout
        /// </summary>
        /// <param name="filename">The file name to upload.</param>
        /// <param name="length">The part of the file to upload.</param>
        /// <param name="timeout">The timeout for performing the uploading operation.</param>
        public void UploadFromFile(string filename, int length, TimeSpan timeout)
        {
            UpDownloadParams uploadParam = new UpDownloadParams(this.blob, filename, 0, length);
            int caluatedTimeout          = GetTimeOutInSeconds(uploadParam.Length);

            this.reqOption.MaximumExecutionTime = (timeout == TimeSpan.MaxValue || timeout.Seconds < caluatedTimeout)
                ? new TimeSpan(0, 0, caluatedTimeout)
                : timeout;
            using (var stream = FabricFile.Open(uploadParam.Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
#if !DotNetCoreClr
                uploadParam.Blob.UploadFromStream(stream, null, this.reqOption);
#else
                uploadParam.Blob.UploadFromStreamAsync(stream, null, this.reqOption, null).Wait();
#endif
            }
        }