Beispiel #1
0
 public override Task FlushAsync(CancellationToken cancellationToken)
 {
     return(_inner.FlushAsync(cancellationToken));
 }
Beispiel #2
0
        private static async Task <CloudBlockBlob> HandleVideoBlob(string videoUrl, CloudBlobContainer serverContainer, HttpClient httpClient, TraceWriter log)
        {
            VideoUrlHelper videoUrlHelper = VideoUrlHelper.Parse(videoUrl);

            CloudBlockBlob videoBlob;

            if (videoUrlHelper != null)
            {
                videoBlob = serverContainer.GetBlockBlobReference(videoUrlHelper.FileName.ToLower());
            }
            else
            {
                throw new ArgumentException("Unable to parse: " + videoUrl);
            }

            videoBlob.Properties.CacheControl = "max-age=31536000";
            if (videoUrlHelper.FileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
            {
                videoBlob.Properties.ContentType = "video/mp4";
            }
            else if (videoUrlHelper.FileName.EndsWith(".mov", StringComparison.OrdinalIgnoreCase))
            {
                videoBlob.Properties.ContentType = "video/quicktime";
            }
            else if (videoUrlHelper.Server.Contains("youtube.com"))
            {
                log.Warning("Video from youtube.com skipped: " + videoUrl);
            }
            else if (videoUrlHelper.Server.Contains("instagram.com"))
            {
                log.Warning("Video from instagram.com skipped: " + videoUrl);
            }
            else if (videoUrlHelper.Server.Contains("vimeo.com"))
            {
                log.Warning("Video from vimeo.com skipped: " + videoUrl);
            }
            else
            {
                throw new ArgumentException("Unexpected ending in: " + videoUrl);
            }

            var response = await httpClient.GetAsync(videoUrl);

            response.EnsureSuccessStatusCode();

            Stream          sourceStream = null;
            CloudBlobStream uploadStream = null;

            try
            {
                sourceStream = await httpClient.GetStreamAsync(videoUrl);

                uploadStream = await videoBlob.OpenWriteAsync();

                byte[] bytes = new byte[64 * 1024];
                int    length;
                do
                {
                    length = await sourceStream.ReadAsync(bytes, 0, 64 * 1024);

                    await uploadStream.WriteAsync(bytes, 0, length);
                }while (length > 0);
            }
            finally
            {
                if (sourceStream != null)
                {
                    sourceStream.Dispose();
                }
                if (uploadStream != null)
                {
                    await uploadStream.FlushAsync();

                    uploadStream.Dispose();
                }
            }

            await videoBlob.FetchAttributesAsync();

            return(videoBlob);
        }
Beispiel #3
0
        static async Task BlobAsync()
        {
            try
            {
                CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient     bc = sa.CreateCloudBlobClient();

                const string       CON_NAME = "smart-blob-container";
                CloudBlobContainer con      = bc.GetContainerReference(CON_NAME);
                bool rr = await con.CreateIfNotExistsAsync();

                const string  blobName = "my.txt";
                CloudPageBlob blob     = con.GetPageBlobReference(blobName);

                bool del = await blob.DeleteIfExistsAsync();

                await blob.CreateAsync(64 *1024, AccessCondition.GenerateIfNotExistsCondition(), new BlobRequestOptions(), new OperationContext());

                using (CloudBlobStream blobStream = await blob.OpenWriteAsync(null, AccessCondition.GenerateEmptyCondition(), new BlobRequestOptions(), new OperationContext()))
                {
                    byte[] sector = new byte[512];
                    for (int ii = 0; ii < sector.Length; ++ii)
                    {
                        sector[ii] = (byte)(ii % 26 + 'A');
                    }
                    MemoryStream mm = new MemoryStream(sector);
                    //byte[] buffer = Encoding.UTF8.GetBytes("zzz宏发科技恢复健康哈尔月UI风雅颂的法尔加zzz----");
                    //await blobStream.WriteAsync(buffer, 0, buffer.Length);
                    //await blobStream.WriteAsync(sector, 0, sector.Length - buffer.Length);
                    await blobStream.WriteAsync(sector, 0, sector.Length);

                    if (blobStream.CanSeek)
                    {
                        blobStream.Seek(1024, System.IO.SeekOrigin.Begin);
                    }

                    //buffer = Encoding.UTF8.GetBytes("this is some test");
                    //await blobStream.WriteAsync(buffer, 0, buffer.Length);
                    //await blobStream.WriteAsync(sector, 0, sector.Length - buffer.Length);
                    await blobStream.WriteAsync(sector, 0, sector.Length);

                    await blobStream.FlushAsync();
                }


                using (Stream blobStream = await blob.OpenReadAsync())
                {
                    byte[] buffer = new byte[2048];

                    const int firstReadCount = 8;
                    int       rc             = await blobStream.ReadAsync(buffer, 0, firstReadCount);

                    if (blobStream.CanSeek)
                    {
                        blobStream.Seek(rc, SeekOrigin.Begin);
                    }
                    rc = await blobStream.ReadAsync(buffer, 8, buffer.Length - firstReadCount);

                    string text = Encoding.UTF8.GetString(buffer);
                    Console.WriteLine(text);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex}");
            }
        }