Exemple #1
0
 private async Task Send4mbBlockAsync(CloudBlockBlob blob, MemoryStream ms, NewBlockId blockId)
 {
     ms.Seek(0, SeekOrigin.Begin);
     await blob.PutBlockAsync(
         blockId : blockId.Base64BlockId,
         blockData : ms,
         contentMD5 : null);
 }
Exemple #2
0
        public async Task UploadBigFileWithCommitsAsync()
        {
            StorageCredentials  creds   = new StorageCredentials(AcctName, AcctKey);
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
            CloudBlobClient     client  = account.CreateCloudBlobClient();
            CloudBlockBlob      blob    = new CloudBlockBlob(Uri, client);

            int blocksize = FOURMB;

            var blockList = new List <string>();

            byte[] bytesRead    = new byte[blocksize];
            int    numBytesRead = 0;

            MemoryStream ms = new MemoryStream();

            ms.Capacity = blocksize;
            Boolean DoneSending = false;

            using (FileStream fs = File.OpenRead(Filename))
            {
                do
                {
                    Console.Write(".");
                    NewBlockId blockId = new NewBlockId();

                    try
                    {
                        ms.Position  = 0;
                        numBytesRead = await fs.ReadAsync(bytesRead, 0, blocksize);

                        if (numBytesRead == blocksize)
                        {
                            ms.Write(bytesRead, 0, blocksize);
                            await Send4mbBlockAsync(blob, ms, blockId);
                        }
                        else
                        {
                            MemoryStream msLast = new MemoryStream();
                            msLast.Capacity = numBytesRead;

                            await msLast.WriteAsync(bytesRead, 0, numBytesRead);
                            await Send4mbBlockAsync(blob, msLast, blockId);

                            DoneSending = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        ;
                    }

                    blockList.Add(blockId.Base64BlockId);
                    await blob.PutBlockListAsync(blockList);
                } while (!DoneSending);
            }
        }
Exemple #3
0
        async Task <ReturnFrom4mbSend> ReadAndPut4mbBlockAsync(FileStream fs, CloudBlockBlob blob)
        {
            byte[] bytesRead    = new byte[FOURMB];
            int    numBytesRead = 0;

            NewBlockId blockId = new NewBlockId();

            Boolean LastBlock = false;

            using (MemoryStream ms = new MemoryStream())
            {
                numBytesRead = await fs.ReadAsync(bytesRead, 0, bytesRead.Length);

                ms.Capacity += numBytesRead;
                await ms.WriteAsync(bytesRead, 0, numBytesRead);

                ms.Position = 0;

                try
                {
                    await blob.PutBlockAsync(blockId : blockId.Base64BlockId,
                                             blockData : ms,
                                             contentMD5 : null,
                                             accessCondition : AccessCondition.GenerateEmptyCondition(),
                                             options : new BlobRequestOptions
                    {
                        StoreBlobContentMD5 = true,
                        UseTransactionalMD5 = true
                    },
                                             operationContext : new OperationContext());
                } catch (Exception ex)
                {
                    ;
                }

                LastBlock = ms.Length != FOURMB;
            }

            ReturnFrom4mbSend r = new ReturnFrom4mbSend(LastBlock, blockId.Base64BlockId);

            return(r);
        }
Exemple #4
0
 private async Task Send4mbBlockAsyncWithAccessCondition(CloudBlockBlob blob, MemoryStream ms, NewBlockId blockId, AccessCondition accessCondition)
 {
     ms.Seek(0, SeekOrigin.Begin);
     await blob.PutBlockAsync(
         blockId : blockId.Base64BlockId,
         blockData : ms,
         contentMD5 : null,
         accessCondition : accessCondition,
         options : new BlobRequestOptions
     {
         StoreBlobContentMD5 = true,
         UseTransactionalMD5 = true
     },
         operationContext : new OperationContext());
 }
Exemple #5
0
        public void UploadBigFileWithCommits()
        {
            StorageCredentials  creds   = new StorageCredentials(AcctName, AcctKey);
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
            CloudBlobClient     client  = account.CreateCloudBlobClient();
            CloudBlockBlob      blob    = new CloudBlockBlob(Uri, client);

            var blockList = new List <string>();

            byte[] bytesRead    = new byte[FOURMB];
            int    numBytesRead = 0;

            MemoryStream ms = new MemoryStream();

            ms.Capacity = FOURMB;
            Boolean DoneSending = false;

            string acquiredLeaseId = null;
            string proposedLeaseId = Guid.NewGuid().ToString();

            using (FileStream fs = File.OpenRead(Filename))
            {
                AccessCondition accessCondition = AccessCondition.GenerateEmptyCondition();
                do
                {
                    Console.Write(".");
                    NewBlockId blockId = new NewBlockId();

                    try
                    {
                        if (blockList.Count > 0)
                        {
                            acquiredLeaseId = ((ICloudBlob)blob).AcquireLeaseAsync(TimeSpan.FromMinutes(1), proposedLeaseId).Result;
                            accessCondition = AccessCondition.GenerateLeaseCondition(acquiredLeaseId);
                        }

                        ms.Position  = 0;
                        numBytesRead = fs.Read(bytesRead, 0, FOURMB);

                        if (numBytesRead == FOURMB)
                        {
                            ms.Write(bytesRead, 0, FOURMB);
                            Send4mbBlockAsyncWithAccessCondition(blob, ms, blockId, accessCondition).Wait();
                        }
                        else
                        {
                            MemoryStream msLast = new MemoryStream();
                            msLast.Capacity = numBytesRead;

                            msLast.Write(bytesRead, 0, numBytesRead);
                            Send4mbBlockAsyncWithAccessCondition(blob, msLast, blockId, accessCondition).Wait();

                            DoneSending = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine();
                        Console.WriteLine($"Error: {ex.Message} :sending block to storage.");
                        continue;
                    }

                    try
                    {
                        blockList.Add(blockId.Base64BlockId);
                        blob.PutBlockListAsync(
                            blockList,
                            accessCondition,
                            options: new BlobRequestOptions
                        {
                            StoreBlobContentMD5 = true,
                            UseTransactionalMD5 = true
                        },
                            operationContext: new OperationContext()).Wait();
                    } catch (Exception ex)
                    {
                        Console.WriteLine();
                        Console.WriteLine($"Error: {ex.Message} :commiting block list.");
                    } finally
                    {
                        if (acquiredLeaseId != null)
                        {
                            ((ICloudBlob)blob).ReleaseLeaseAsync(accessCondition).Wait();
                            acquiredLeaseId = null;
                        }
                    }
                } while (!DoneSending);
            }
        }