如何使用新的Azure存储SDK v12打开到新Blob的可写流?

时间:2020-07-27 20:16:10

标签: c# azure azure-storage azure-storage-blobs

我正在使用Azure Storage SDK v12,并且正在寻找一种将流打开到特定Blob的方法,就像以前的版本一样:

OpenWrite

问题是我在新API中找不到Stream方法,该方法无法使我访问public async Task RunAsync( [BlobTrigger("data/{listId}/{name}.txt", Connection = "storageAccountConnectionString")] Stream sourceStream, string listId, string name, [Blob("data/{listId}/processed/{name}.txt", FileAccess.Write, Connection = "storageAccountConnectionString")] Stream destinationStream) { // Here I use the destinationStream instance to upload data as it is being processed. It prevents me from loading everything in memory before calling the UploadAsync method of a BlobClient instance. }

问题

如何使用新的Azure Storage SDK v12打开到Blob的可写流?


解决方法

由于我正在使用Azure Functions,因此有一种解决方法,涉及使用函数的Blob作为Out参数:

| Year | Month | Date | Formatted_Date | Formatted_Date2 |   Q2   |
|------|-------|------|----------------|-----------------|--------|
| 2018 |   6   |  10  |   2018-06-10   |       17692     |    4   |
| 2018 |   6   |  12  |   2018-06-12   |       17694     |    3   | 
| 2018 |   6   |  14  |   2018-06-14   |       17696     |    2   |
| 2018 |   6   |  18  |   2018-06-18   |       17700     |    4   |
| 2018 |   6   |  21  |   2018-06-21   |       17703     |    3   |
| 2018 |   7   |  22  |   2018-07-22   |       17734     |    1   |
| 2018 |   7   |  23  |   2018-07-24   |       17736     |    1   |

2 个答案:

答案 0 :(得分:6)

您需要使用 Azure.Storage.Blobs.Specialized 命名空间中的 GetBlockBlobClient 扩展方法:

public async Task<Stream> CreateOutputStream(string fullFilePath)
{
    var blockBlobClient = _blobContainerClient.GetBlockBlobClient(fullFilePath);
    var stream = await blockBlobClient.OpenWriteAsync(true);

    return stream;
}

答案 1 :(得分:1)

尝试一下

BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync("zipfiles");
BlobClient blobClient = containerClient.GetBlobClient(Guid.NewGuid().ToString());

using(FileStream strem = File.OpenRead("10_million_lines.file")){
   await blobClient.UploadAsync(strem , true);
}