简体   繁体   English

在上传到Azure Blob存储之前对图像数据进行加密

[英]Encrypting image data before uploading to azure blob storage

I have the following code that uploads an image to Azure blob storage. 我有以下代码将映像上传到Azure blob存储。 I would like to encrypt the image data before uploading to the blob. 我想在上传到Blob之前对图像数据进行加密。 I already have a helper class for encrypting and decrypting that I can use by calling AESEncryption.Encrypt("plainText", "key", salt"); 我已经有了一个用于加密和解密的助手类,可以通过调用AESEncryption.Encrypt(“ plainText”,“ key”,salt“);来使用。

I'm just trying to figure out how tom integrate my encryption method into the code. 我只是想弄清楚如何将加密方法集成到代码中。 Also, I'm guessing that once it's encrypted instead of calling blob.UploadFromFile() I will be calling blob.UploadFromByteArray(). 另外,我猜想一旦将其加密而不是调用blob.UploadFromFile(),我将调用blob.UploadFromByteArray()。

public override Task ExecutePostProcessingAsync()
    {
        try
        {
            // Upload the files to azure blob storage and remove them from local disk
            foreach (var fileData in this.FileData)
            {
                var filename = BuildFilename(Path.GetExtension(fileData.Headers.ContentDisposition.FileName.Trim('"')));

                // Retrieve reference to a blob
                var blob = _container.GetBlockBlobReference(filename);
                blob.Properties.ContentType = fileData.Headers.ContentType.MediaType;
                blob.UploadFromFile(fileData.LocalFileName, FileMode.Open);
                File.Delete(fileData.LocalFileName);
                Files.Add(new FileDetails
                {
                    ContentType = blob.Properties.ContentType,
                    Name = blob.Name,
                    Size = blob.Properties.Length,
                    Location = blob.Uri.AbsoluteUri
                });
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return base.ExecutePostProcessingAsync();
    }

As I see it, you could do it 3 ways: 如我所见,您可以通过3种方式做到这一点:

  1. Encrypt the file beforehand and then upload that encrypted file. 事先加密文件,然后上传该加密文件。
  2. As you mentioned, you could read the file in byte array and then encrypt that byte array and upload it using UploadFromByteArray method. 如前所述,您可以读取字节数组中的文件,然后对该字节数组进行加密,然后使用UploadFromByteArray方法将其上传。
  3. Similar to #2 but instead of uploading byte array, you could rely on streams and upload using UploadFromStream method. 与#2相似,但是您可以依赖于流并使用UploadFromStream方法上传,而不是上传字节数组。

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

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