gpt4 book ai didi

javascript - 您可以从函数绑定(bind)中读取连接字符串吗?

转载 作者:行者123 更新时间:2023-12-01 00:41:18 25 4
gpt4 key购买 nike

我们可以绑定(bind)到连接字符串值,以便简单地输出绑定(bind)到特定存储帐户吗?

我想创建一个 blob mover,消费者将在其中传递一个对象,例如:

{
"sourceConnection":"..blob connection string",
"destinationConnection":"...blob connection string",
"filename":"/someContainer/someDir/myfile.jpg"
}

该函数将由队列触发:

    {
"name": "myQueueItem",
"queueName": "myqueue-items",
"connection":"",
"type": "queueTrigger",
"direction": "in"
}

...其中 myQueueItem 将具有如上所述的有效负载。

我们如何将 blob 从 sourceConnection 移动到 destinationConnection

我指定了 javascriptc# 标记,因为我需要在任一标记中制定解决方案。

如果我们假设源存储帐户和目标存储帐户相同,我们可以简单地编写一个函数来在两个容器之间移动 blob,如下所示:

public static class BlobMover
{
[StorageAccount("Connection")]
[FunctionName("BlobMover")]
public static async Task Run(
[QueueTrigger("%BlobMover:TriggerQueue%")] BlobMessage msg,
[Blob("{source}-error/{name}", FileAccess.Write)] CloudBlockBlob error,
[Blob("{destination}/{name}", FileAccess.Write)] CloudBlockBlob @out,
[Blob("{source}/{name}", FileAccess.Read)] CloudBlockBlob @in,
ILogger log)
{
await @out.StartCopyAsync(@in);
}
}

但是,存储帐户会有所不同,因此上述解决方案不起作用。

最佳答案

假设您使用 HTTP 触发 Azure 函数接收有效负载输入:

public class Payload
{
public string SourceContainer { get; set; }
public string DestinationContainer { get; set; }
public string SourceConnection{ get; set; }
public string DestinationConnection{ get; set; }
}

现在接收请求中的信息:

var payload = req.Content.ReadAsAsync<Payload>();
var sourceConnection = payload.SourceConnection;
var destConnection = payload.DestinationConnection;
var sourceContainer = payload.SourceContainer;
var destContainer= payload.DestinationContainer;

创建sourceClientdestinationClient:

#SOURCE
CloudStorageAccount sourceAccount = CloudStorageAccount.Parse(sourceConnection);
CloudBlobClient sourceClient = sourceAccount.CreateCloudBlobClient();

#DEST
CloudStorageAccount destAccount = CloudStorageAccount.Parse(destConnection);
CloudBlobClient destClient = destAccount .CreateCloudBlobClient();

从源下载

CloudBlobContainer sourceBlobContainer = sourceClient.GetContainerReference(sourceContainer);
ICloudBlob sourceBlob = await sourceBlobContainer.GetBlobReferenceFromServerAsync("MyBlob.doc");
await sourceBlob.DownloadToFileAsync("MyFile.doc", FileMode.Create);

上传到目的地

CloudBlobContainer destBlobContainer = destClient.GetContainerReference(destContainer);
CloudBlockBlob destBlob = destBlobContainer.GetBlockBlobReference("NewBlob.doc");
await destBlob.UploadFromFileAsync("MyFile.doc");

关于javascript - 您可以从函数绑定(bind)中读取连接字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57697355/

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