gpt4 book ai didi

.net - 在不使用 azcopy 的情况下跨 Azure 容器复制文件

转载 作者:行者123 更新时间:2023-12-03 05:36:00 27 4
gpt4 key购买 nike

我想在不使用 azcopy 实用程序的情况下跨 Azure 容器复制文件。请帮助编写脚本(Spring boot 或 .net 示例)。

我能够从容器上传\下载文件到本地。现在想知道如何实现远程复制。

以下代码工作正常。我现在需要的只是远程复制。

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Azure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;

namespace whizlabblob
{
class Program
{
static string storageconnstring = "DefaultEndpointsProtocol=https;AccountName=mainices;AccountKey=********************";
static string containerName = "demo";
static string filename = "sample.txt";
static string filepath="C:\\Work\\sample.txt";
static string downloadpath = "C:\\Work\\sample2.txt";
static async Task Main(string[] args)
{
//Container().Wait();
//CreateBlob().Wait();
//GetBlobs().Wait();
// GetBlob().Wait();
Console.WriteLine("Complete");
Console.ReadKey();
}

static async Task Container()
{

BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);
}



static async Task CreateBlob()
{

BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

BlobClient blobClient = containerClient.GetBlobClient(filename);
using FileStream uploadFileStream = File.OpenRead(filepath);


await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();
}


static async Task GetBlobs()
{

BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);


await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
{
Console.WriteLine("\t" + blobItem.Name);
}

}

static async Task GetBlob()
{

BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);

BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

BlobClient blob = containerClient.GetBlobClient(filename);

BlobDownloadInfo blobdata = await blob.DownloadAsync();


using (FileStream downloadFileStream = File.OpenWrite(downloadpath))
{
await blobdata.Content.CopyToAsync(downloadFileStream);
downloadFileStream.Close();
}


// Read the new file
using (FileStream downloadFileStream = File.OpenRead(downloadpath))
{
using var strreader = new StreamReader(downloadFileStream);
string line;
while ((line = strreader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}

}
}
}

最佳答案

你就快到了:)。为了将 Blob 从一个容器复制到另一个容器,您只需为目标容器中的 Blob 创建一个 BlobClient 实例并调用 StartCopyFromUriAsync方法并传递源 blob 的 URI。

这是执行此操作的代码:

    static async Task CopyBlob()
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

BlobClient blobClient = containerClient.GetBlobClient(filename);
var blobUri = blobClient.Uri;

BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
await targetBlobClient.StartCopyFromUriAsync(blobUri);
}

需要记住的一些事情:

  • 由于您要在同一存储帐户中复制 Blob,因此您只需要源 Blob 的 URI。但是,如果您要跨存储帐户复制 blob,则需要确保为复制方法提供的 URI 应可公开访问。
  • 同样,由于您在同一存储帐户中复制 blob,因此服务器端复制操作是同步的。如果跨存储帐户复制 blob,则该操作是异步的,可能需要一些时间才能完成。您必须等待复制操作完成,然后才能对源 blob 执行任何操作(例如删除它)。

关于.net - 在不使用 azcopy 的情况下跨 Azure 容器复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62586809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com