简体   繁体   中英

Error when copying a blob from one container to another, the content final is 0 bytes in C#

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($"{name}");
            var destinationContainer = client.GetContainerReference("testoutput");
            var destinationBlob = destinationContainer.GetBlockBlobReference($"{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.

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($"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();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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