Azure 存储创建临时文件

问题描述 投票:0回答:1

我正在使用 Azure 存储来存储我的 .NET Core 应用程序的文件。我需要保存临时文件的可能性。

我正在这样上传文件:

public async Task UploadFile(Stream fileStream, string path)
{
  var connectionString = "connection string";
  BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
  BlobContainerClient blobContainerClient = 
  blobServiceClient.GetBlobContainerClient("files");
  BlobClient blobClient = blobContainerClient.GetBlobClient(path);
  fileStream.Position = 0;
  BlobUploadOptions uploadOptions = new BlobUploadOptions();
  var extension = path.Split(".").LastOrDefault();
  if (!string.IsNullOrEmpty(extension) && MimeTypes.Values.ContainsKey(extension))
  {
    var contentType = MimeTypes.Values[extension];
    var blobHttpHeader = new BlobHttpHeaders { ContentType = contentType };
    uploadOptions.HttpHeaders = blobHttpHeader;
  }

  await blobClient.UploadAsync(fileStream, uploadOptions);
}

有没有办法创建一个小时后自动删除的文件?

.net-core azure-storage
1个回答
0
投票

有没有办法创建一个小时后自动删除的文件?

您可以使用以下代码上传 Blob,并使用 C# 在 Azure 存储中通过

request
方法设置 Blob 相对于当前时间的过期日期。

代码:

using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        string url = "https://venkat8912.blob.core.windows.net";
        string containerName = "test";
        string blobName = "demo.png";
        string SAStoken = "<Sas token>"; 
        DateTime dt = DateTime.UtcNow;

        // The file you want to upload
        string filePath = @"C:\Downloads\test.png";

        using (HttpClient httpClient = new HttpClient())
        {
            // Upload the Blob
            HttpRequestMessage uploadRequest = new HttpRequestMessage(HttpMethod.Put, new Uri($"{ url}/{containerName}/{blobName}?{SAStoken}"));
            byte[] fileBytes = await File.ReadAllBytesAsync(filePath);
            uploadRequest.Content = new ByteArrayContent(fileBytes);
            uploadRequest.Headers.Add("x-ms-blob-type", "BlockBlob");
            HttpResponseMessage uploadResponse = await httpClient.SendAsync(uploadRequest);

            if (uploadResponse.IsSuccessStatusCode)
            {
                Console.WriteLine("Blob uploaded successfully");

                // Set the Expiry Time
                HttpRequestMessage expiryRequest = new HttpRequestMessage(HttpMethod.Put, new Uri($"{url}/{containerName}/{blobName}?comp=expiry&{SAStoken}"));
                expiryRequest.Headers.Add("x-ms-version", "2020-02-10");
                expiryRequest.Headers.Add("x-ms-expiry-time", "3600000"); // one hour
                expiryRequest.Headers.Add("x-ms-expiry-option", "RelativeToNow");
                expiryRequest.Headers.Add("x-ms-date", dt.ToString("R"));

                HttpResponseMessage expiryResponse = await httpClient.SendAsync(expiryRequest);

                if (expiryResponse.IsSuccessStatusCode)
                {
                    Console.WriteLine("Expiry time added to the blob successfully");
                }
                else
                {
                    Console.WriteLine($"Error setting expiry: {expiryResponse.StatusCode} - {await expiryResponse.Content.ReadAsStringAsync()}");
                }
            }
            else
            {
                Console.WriteLine($"Error uploading blob: {uploadResponse.StatusCode} - {await uploadResponse.Content.ReadAsStringAsync()}");
            }
        }
    }
}

输出:

Blob uploaded successfully
Expiry time added to the blob successfully

enter image description here

仅在启用了存储帐户的

hierarchical namespace
上才允许执行上述代码操作。

参考: 设置 Blob 过期 (REST API) - Azure 存储 |微软学习

© www.soinside.com 2019 - 2024. All rights reserved.