【问题标题】:Add tag to blob using java SDK使用 java SDK 将标签添加到 blob
【发布时间】:2021-09-27 03:49:39
【问题描述】:

我正在尝试使用 java SDK 将索引标签添加到我在 azure Blob Storage 上的数据。我需要添加标签,以便更轻松地过滤并实施生命周期策略。当我上传一个文件时,我希望该文件添加一个标签键 x 映射一个值 y。我找不到任何有用的 java 文档,我找到的最接近的东西是 C# 中的this。另外,我确实找到了函数setTags (this too),它看起来像我想要的,但我无法让它工作(文件似乎是在没有标签的情况下上传的)。我试过以下代码:

String connectStr = System.getenv("AZURE_STORAGE_CONNECTION_STRING");
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
BlobContainerClient container = blobServiceClient.getBlobContainerClient("tcpblobstorage");
BlobClient blobClient = container.getBlobClient(path + fileName);
Map<String, String> map = new HashMap<>();
map.put("Client", "Client");
blobClient.setTags(map);
blobClient.upload(is, length, true);

注意:path + fileName 是即将上传到 azure 的新文件的路径。

有人对我可能做错的事情有任何指示或想法吗?

【问题讨论】:

    标签: java azure azure-blob-storage


    【解决方案1】:

    【讨论】:

    • 感谢您的快速回复!我试试看。
    【解决方案2】:

    我已经接受了@Gaurav 的回答,但如果有人来找它,这里是更新后的代码:

    String connectStr = System.getenv("AZURE_STORAGE_CONNECTION_STRING");
    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
    BlobContainerClient container = blobServiceClient.getBlobContainerClient("tcpblobstorage");
    BlobClient blobClient = container.getBlobClient(path + fileName);
    Map<String, String> map = new HashMap<>();
    map.put("Client", "Client");
    BlockBlobSimpleUploadOptions options = new BlockBlobSimpleUploadOptions(is, length).setTags(map);
    blobClient.getBlockBlobClient().uploadWithResponse(options, Duration.ofSeconds(30L), Context.NONE);
    

    【讨论】:

      【解决方案3】:

      另一种选择是在上传后使用setTags。这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多