如何将随机字符串添加到Azure Function Blob输出文件名? -指定的阻止列表无效

问题描述 投票:0回答:1

当以非快速周期调用java函数时,我的Blob输出成功运行。但是在快速循环中调用函数时会出错。由于Java不支持持久单例,因此我只需要使每个文件都具有唯一的名称即可。

如何为名称添加随机字符串或guid,以使每个文件都获得唯一的名称,即使时间戳是相同的。

错误:

system.Private.CoreLib: Exception while executing function: Functions.TopicTriggerDatalakeOutput. 
Microsoft.Azure.WebJobs.Host: Error while handling parameter _binder after function returned:. 
Microsoft.WindowsAzure.Storage: The specified block list is invalid.

CODE:

@BlobOutput(
         name = "target", 
        connection = "DataLakeConnString",
        path = "profile/blob_{DateTime}.csv")
        OutputBinding<String> outputItem,
azure-functions
1个回答
0
投票

基于Java的Azure函数声明部分中提供的Blob名称必须为常量,因此,如果要在绑定中直接向Blob添加随机GUID,则不可能。

但是您的想法仍然可以实现,只需将逻辑放在函数中,就像这样:

String connectStr = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxx;EndpointSuffix=core.windows.net";
String containerName = "yourcontainername";
String fileName = "yourblobprefix" + java.util.UUID.randomUUID() + ".txt";
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
BlobContainerClient containerClient = blobServiceClient.createBlobContainer(containerName);
BlobClient blobClient = containerClient.getBlobClient(fileName);
long streamlong = request.getBody().get().length();
String st = request.getBody().get();
InputStream inputstream = new ByteArrayInputStream(st.getBytes());
blobClient.upload(inputstream, streamlong, true);

您可以看一下此doc。它告诉您如何上传Blob,它基于Java v12 sdk。 This关于Java SDK v8。

© www.soinside.com 2019 - 2024. All rights reserved.