将 blob 从一个容器复制到另一个容器时出错,C# 中最终内容为 0 字节

发布于 2025-01-10 22:36:30 字数 1437 浏览 0 评论 0原文

我正在开发一个项目,使用 Azure 函数和 C# 将 blob 从一个容器移动到另一个容器,我尝试了不同的方法将文件从一个容器复制到另一个容器,但是只能移动名称和扩展名,但是下载或尝试访问文件时,内容为 0 字节。

这是当前实现的代码。


namespace TestInput
{
    [StorageAccount ("BlobConnectionString")]
    public class TestInput
    {
        [FunctionName("TestInput")]
        public static void Run(
            [BlobTrigger("test/{name}")] Stream myBlob,
            [Blob("testoutput/{name}", FileAccess.Write)] Stream outputBlob,
            string name,
            
                            ILogger log)
        {
            
            var accountName = Environment.GetEnvironmentVariable("AccountName");
            var accountKey = Environment.GetEnvironmentVariable("AccountKey");
            var cred = new StorageCredentials(accountName, accountKey);
            var account = new CloudStorageAccount(cred, true);
            var client = account.CreateCloudBlobClient();
            var sourceContainer = client.GetContainerReference("test");
            var sourceBlob = sourceContainer.GetBlockBlobReference($"{name}");
            var destinationContainer = client.GetContainerReference("testoutput");
            var destinationBlob = destinationContainer.GetBlockBlobReference($"{name}");
            destinationBlob.UploadFromStream(myBlob);
            sourceBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
            
        }
    }

}

如果您能告诉我如何解决这个问题或者我缺少什么参数,我将不胜感激。

I am working on a project to move a blob from one container to another, using azure functions with C#, I have tried different ways to copy the file from one container to another, however it has only been possible to move the name and extension but when downloading or trying to access the file the content is 0 bytes.

This is the code currently implemented.


namespace TestInput
{
    [StorageAccount ("BlobConnectionString")]
    public class TestInput
    {
        [FunctionName("TestInput")]
        public static void Run(
            [BlobTrigger("test/{name}")] Stream myBlob,
            [Blob("testoutput/{name}", FileAccess.Write)] Stream outputBlob,
            string name,
            
                            ILogger log)
        {
            
            var accountName = Environment.GetEnvironmentVariable("AccountName");
            var accountKey = Environment.GetEnvironmentVariable("AccountKey");
            var cred = new StorageCredentials(accountName, accountKey);
            var account = new CloudStorageAccount(cred, true);
            var client = account.CreateCloudBlobClient();
            var sourceContainer = client.GetContainerReference("test");
            var sourceBlob = sourceContainer.GetBlockBlobReference(
quot;{name}");
            var destinationContainer = client.GetContainerReference("testoutput");
            var destinationBlob = destinationContainer.GetBlockBlobReference(
quot;{name}");
            destinationBlob.UploadFromStream(myBlob);
            sourceBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
            
        }
    }

}

I would be grateful if you could tell me how to solve this problem or what parameter I am missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2025-01-17 22:36:30

请检查以下代码是否有助于使用 Azure Function 将 Blob 从一个容器复制到另一个容器:

以下是 Blob 存储触发器类型的 .NET 6 Azure Function:

using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;

namespace KrishBlobTriggerAF1205
{
    public class Function1
    {
        [FunctionName("Function1")]
        public async Task RunAsync([BlobTrigger("dev/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log,
            [Blob("staging/{name}", FileAccess.Write)] Stream outputBlob)
        {
            var srcconnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            string sourceContainer = "source";
            string targetContainer = "target";
            string blobName = "blob-name.txt";


            BlobServiceClient serviceClient = new BlobServiceClient(srcconnectionString);
            BlobContainerClient sourceContainerClient = serviceClient.GetBlobContainerClient(sourceContainer);
            BlobContainerClient targetContainerClient = serviceClient.GetBlobContainerClient(targetContainer);
            BlobClient sourceBlobClient = sourceContainerClient.GetBlobClient(blobName);
            BlobClient targetBlobClient = targetContainerClient.GetBlobClient(blobName);
            log.LogInformation("Sending copy blob request....");
            var result = await targetBlobClient.StartCopyFromUriAsync(sourceBlobClient.Uri);
            log.LogInformation("Copy blob request sent....");
            log.LogInformation("============");
            bool isBlobCopiedSuccessfully = false;
            do
            {
                log.LogInformation("Checking copy status....");
                var targetBlobProperties = await targetBlobClient.GetPropertiesAsync();
                log.LogInformation($"Current copy status = {targetBlobProperties.Value.CopyStatus}");
                if (targetBlobProperties.Value.CopyStatus.Equals(CopyStatus.Pending))
                {
                    System.Threading.Thread.Sleep(1000);
                }
                else
                {
                    isBlobCopiedSuccessfully = targetBlobProperties.Value.CopyStatus.Equals(CopyStatus.Success);
                    break;
                }
            } while (true);

            if (isBlobCopiedSuccessfully)
            {
                log.LogInformation("Blob copied successfully. Now deleting source blob...");
                await sourceBlobClient.DeleteAsync();
            }
        }
    }
}

Please Check if the below code helps to copy a blob from one container to another using Azure Function:

Below is the .NET 6 Azure Function of type Blob Storage Trigger:

using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;

namespace KrishBlobTriggerAF1205
{
    public class Function1
    {
        [FunctionName("Function1")]
        public async Task RunAsync([BlobTrigger("dev/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log,
            [Blob("staging/{name}", FileAccess.Write)] Stream outputBlob)
        {
            var srcconnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            string sourceContainer = "source";
            string targetContainer = "target";
            string blobName = "blob-name.txt";


            BlobServiceClient serviceClient = new BlobServiceClient(srcconnectionString);
            BlobContainerClient sourceContainerClient = serviceClient.GetBlobContainerClient(sourceContainer);
            BlobContainerClient targetContainerClient = serviceClient.GetBlobContainerClient(targetContainer);
            BlobClient sourceBlobClient = sourceContainerClient.GetBlobClient(blobName);
            BlobClient targetBlobClient = targetContainerClient.GetBlobClient(blobName);
            log.LogInformation("Sending copy blob request....");
            var result = await targetBlobClient.StartCopyFromUriAsync(sourceBlobClient.Uri);
            log.LogInformation("Copy blob request sent....");
            log.LogInformation("============");
            bool isBlobCopiedSuccessfully = false;
            do
            {
                log.LogInformation("Checking copy status....");
                var targetBlobProperties = await targetBlobClient.GetPropertiesAsync();
                log.LogInformation(
quot;Current copy status = {targetBlobProperties.Value.CopyStatus}");
                if (targetBlobProperties.Value.CopyStatus.Equals(CopyStatus.Pending))
                {
                    System.Threading.Thread.Sleep(1000);
                }
                else
                {
                    isBlobCopiedSuccessfully = targetBlobProperties.Value.CopyStatus.Equals(CopyStatus.Success);
                    break;
                }
            } while (true);

            if (isBlobCopiedSuccessfully)
            {
                log.LogInformation("Blob copied successfully. Now deleting source blob...");
                await sourceBlobClient.DeleteAsync();
            }
        }
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文