繁体   English   中英

Azure Blob Storage 文件上传块

[英]Azure Blob Storage file upload in blocks

我正在编写一个 C# 控制台应用程序,它将本地文件上传到 Azure Blob 存储。

在上传大文件时(比如 pdf 文件)我在做两件事

  1. 将文件分成块。
  2. 在 azure 云存储上暂存块时使用 SemphoreSlim。

有什么方法可以测试块是否按预期的顺序排列,并且 pdf 在提交块后没有损坏?

我尝试的是在上传并验证签名后再次读取文件,但是如果某些页面或 pdf 的某些部分丢失了如何检测此类问题。

在Azure创建存储账户和容器在此处输入图像描述

在存储帐户中创建容器在此处输入图像描述

使用信号量或线程,我们可以在 azure 中上传文件块

            string mystrconnectionString = "Connection String";
            string containerName = "container Name";
            var containerClient = new BlobContainerClient(mystrconnectionString, containerName);
           string path = txtBrowsepath.Text;

            Stopwatch timer = Stopwatch.StartNew();

            try
            {
                BlobUploadOptions options = new BlobUploadOptions
                {
                    TransferOptions = new StorageTransferOptions
                    {
                        MaximumConcurrency = 8,

                        MaximumTransferSize = 50 * 1024 * 1024
                    }
                };

                var tasks = new Queue<Task<Response<BlobContentInfo>>>();
               
                var blockBlob_Client = containerClient.GetBlockBlobClient("Sample.pdf");

                tasks.Enqueue(await blockBlob_Client.UploadAsync(path, options));

                await Task.WhenAll(tasks);

                timer.Stop();
               MessageBox.Show("Uploaded files in {timer.Elapsed.TotalSeconds} " timer +" seconds");

Azure 分块上传文件在此处输入图像描述

检查文件是否损坏
在此处输入图像描述

将现有的.jpg文件修改为.pdf并检查文件是否损坏,然后它显示为损坏的文件 - 正如预期的那样。

在此处输入图像描述

下面是检查 pdf 文件是否损坏的代码

private void button3_Click(object sender, EventArgs e)
            {
                if(IsPDFCorrupted(txtBrowse.Text))
                { 
                    label2.Text = "File Not Corrupted!";
                }
                else
                {
                    label2.ForeColor= Color.Red;
                    label2.Text = "File Corrupted!";
                }
    
            }
            public bool IsPDFCorrupted(string fileName)
            {
                byte[] bf = null;
                FileStream file_stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(file_stream);
                long numBytes = new FileInfo(fileName).Length;
                bf = br.ReadBytes(5);
    
                var enc = new ASCIIEncoding();
                var header = enc.GetString(bf);
    
                if (bf[0] == 0x25 && bf[1] == 0x50
                    && bf[2] == 0x44 && bf[3] == 0x46)
                {
                    return header.StartsWith("%PDF-");
                }
                return false;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM