问题描述

查看Java Storage SDK,想找一个 uploadWithResponse  的示例代码,但是通过全网搜索,结果没有任何有帮助的代码。使用最近ChatGPT来寻求答案,得到非常有格式的内容:

问:java azure storage account to use uploadWithResponse

答:To use the uploadWithResponse method with the Azure Blob Storage client library for Java, follow these steps:

  1. Add the Azure Storage Blob dependency to your project's build file. For Maven, add the following to your pom.xml:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.14.2</version>
</dependency>
  1. Import the necessary classes:
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
  1. Create a BlobServiceClient instance using your storage account connection string:
String connectionString = "your_connection_string";
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
  1. Get a reference to the container and the blob you want to upload:
String containerName = "your_container_name";
String blobName = "your_blob_name";
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
BlobClient blobClient = containerClient.getBlobClient(blobName);
  1. Upload a file using the uploadWithResponse method:
import com.azure.storage.blob.models.BlobUploadFromFileOptions;
import java.nio.file.Path;
import java.nio.file.Paths; Path filePath = Paths.get("path/to/your/file");
BlobUploadFromFileOptions options = new BlobUploadFromFileOptions(filePath);
blobClient.uploadWithResponse(options, null, null);

This will upload the file to the specified blob in the Azure Blob Storage container. Remember to replace the placeholders with your actual connection string, container name, blob name, and file path.

继续追问:BlobParallelUploadOptions with string content

答:(前面4步的答案一样,第五步的内容为)

  1. Convert the string content to an InputStream and set up BlobParallelUploadOptions:
String content = "your_string_content";
InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
long blockSize = 100 * 1024L; // 100 KB
int maxConcurrency = 5;
ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions(blockSize, maxConcurrency);
BlobParallelUploadOptions options = new BlobParallelUploadOptions(inputStream).setParallelTra

在继续问:如何使用 “To avoid overwriting, pass "*" to setIfNoneMatch(String ifNoneMatch). how to use the setifnonematch” 

答:(前面4步的答案一样,第五步的内容为)
  1. Set up BlobUploadOptions with setIfNoneMatch:
BlobUploadOptions options = new BlobUploadOptions()
.setInputStream(inputStream)
.setLength((long) content.length())
.setHttpHeaders(new BlobHttpHeaders().setContentType("text/plain"))
.setRequestConditions(new BlobRequestConditions().setIfNoneMatch("*"));
  1. Upload the string content using the uploadWithResponse method with BlobUploadOptions:
blobClient.uploadWithResponse(options, null, null);

This will upload the string content to the specified blob in the Azure Blob Storage container only if the blob does not already exist. If the blob exists, the upload will fail with a BlobStorageException due to the setIfNoneMatch("*") condition. Remember to replace the placeholders with your actual connection string, container name, blob name, and string content.

问题解答

根据ChatGPT提供的三部分信息,通过人力拼接和测试。得到了成功执行的uploadWithResponse代码:

uploadWithResponse示例代码

                String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=***************";
      String containerName = "******************";
       String fileName = "******************"; BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(storageConnectionString)
.buildClient();
BlobContainerClient containerClient = blobServiceClient.createBlobContainer(containerName);
BlobClient blobClient = containerClient.getBlobClient(fileName);

//准备content并且初始化blob parallel upload options对象
  String content = "Test to append new content into exists blob! by blogs lu bian liang zhan deng ";
InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
BlobParallelUploadOptions options = new BlobParallelUploadOptions(inputStream, content.getBytes().length);
options.setRequestConditions(new BlobRequestConditions().setIfNoneMatch("*"));
Response<BlockBlobItem> rsp = blobClient.uploadWithResponse(options, null, null); if(rsp.getStatusCode()==201)
{
System.out.println("append content successful........");
}

说明:

1) content 中为需要写入Blob的内容

2) 把string转换为以UTF_8编码的input stream

3) 根据 input stream来初始化 blob paralle upload options对象

4) 设置 Request Conditions,当不需要重写的时候,可以使用 setIfNoneMatch("*")。如果操作的文件存在,则会出现  Status code 409, BlobAlreadyExistss 提示。

5) 调用upload with response方法,获取返回值,如果 返回值得status code为 201,表示Storage Account接受了这次 blob 内容的改动。

运行效果展示图

参考资料

BlobClient Class:https://learn.microsoft.com/en-us/java/api/com.azure.storage.blob.BlobClient?view=azure-java-stable

BlobRequestConditions Class:https://learn.microsoft.com/en-us/java/api/com.azure.storage.blob.models.blobrequestconditions?view=azure-java-stable#com-azure-storage-blob-models-blobrequestconditions-setifnonematch(java-lang-string)

适用于 Java 的 Azure Blob 存储客户端库 : https://docs.azure.cn/zh-cn/storage/blobs/storage-quickstart-blobs-java?tabs=powershell%2Cmanaged-identity%2Croles-azure-portal%2Csign-in-azure-cli#upload-blobs-to-a-container

 

【Azure 存储服务】Java Storage SDK 调用 uploadWithResponse 代码示例(询问ChatGTP得代码原型后人力验证)的更多相关文章

  1. 【Azure 存储服务】代码版 Azure Storage Blob 生成 SAS (Shared Access Signature: 共享访问签名)

    问题描述 在使用Azure存储服务,为了有效的保护Storage的Access Keys.可以使用另一种授权方式访问资源(Shared Access Signature: 共享访问签名), 它的好处可 ...

  2. 【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob

    问题描述 在使用Azure的存储服务时候,如果上传的文件大于了100MB, 1GB的情况下,如何上传呢? 问题解答 使用Azure存储服务时,如果要上传文件到Azure Blob,有很多种工具可以实现 ...

  3. 解读 Windows Azure 存储服务的账单 – 带宽、事务数量,以及容量

    经常有人询问我们,如何估算 Windows Azure 存储服务的成本,以便了解如何更好地构建一个经济有效的应用程序.本文我们将从带宽.事务数量,以及容量这三种存储成本的角度探讨这一问题. 在使用 W ...

  4. 玩转Windows Azure存储服务——网盘

    存储服务是除了计算服务之外最重要的云服务之一.说到云存储,大家可以想到很多产品,例如:AWS S3,Google Drive,百度云盘...而在Windows Azure中,存储服务却是在默默无闻的工 ...

  5. 【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed

    问题描述 使用Azure Storage Account的共享访问签名(Share Access Signature) 生成的终结点,连接时遇见  The Azure Storage endpoint ...

  6. 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例

    什么是表存储 Azure 表存储是一项用于在云中存储结构化 NoSQL 数据的服务,通过无结构化的设计提供键/属性存储. 因为表存储无固定的数据结构要求,因此可以很容易地随着应用程序需求的发展使数据适 ...

  7. 玩转Windows Azure存储服务——高级存储

    在上一篇我们把Windows Azure的存储服务用作网盘,本篇我们继续挖掘Windows Azure的存储服务——高级存储.高级存储自然要比普通存储高大上的,因为高级存储是SSD存储!其吞吐量和IO ...

  8. 【Azure 存储服务】Hadoop集群中使用ADLS(Azure Data Lake Storage)过程中遇见执行PUT操作报错

    问题描述 在Hadoop集中中,使用ADLS 作为数据源,在执行PUT操作(上传文件到ADLS中),遇见 400错误[put: Operation failed: "An HTTP head ...

  9. 【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)

    问题描述 如何把开启NFS协议的Azure Blob挂载到Linux虚拟机中呢? [答案]:可以使用 NFS 3.0 协议从基于 Linux 的 Azure 虚拟机 (VM) 或在本地运行的 Linu ...

  10. oracle顺序控制语句goto、null和分页过程中输入输出存储、java程序的调用过程

    顺序控制语句1 goto建议不要使用 declare i number:=; begin loop dbms_output.put_line(i); then goto end_loop; end i ...

随机推荐

  1. Abp学习(一) abp+vue +mysql框架搭建

    一.到Abp官网下载框架 地址:https://aspnetboilerplate.com/Templates 二.打开项目 修改数据库连接为MySql,默认是SQL Server 2.1.修改链接字 ...

  2. layui tree 未命名 所遇到的坑

    由于传入类型不对,需要传入object类型,我传入的是String类型,所以出现了 一堆未命名, 在控制台输出查看,是一模一样的,困扰了我一下午,再次感谢老哥 参考自:https://www.cnbl ...

  3. 痞子衡嵌入式:内存读写正确性压力测试程序(memtester)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是内存读写正确性压力测试程序memtester. 在嵌入式系统中,内存(RAM)的重要性不言而喻,系统性能及稳定性都与内存息息相关.关于内 ...

  4. Double-Checked Locking 双重检查锁问题

    Code Correctness: Double-Checked Locking Abstract Double-checked locking 是一种不正确的用法,并不能达到预期目标. Explan ...

  5. 19.new和delete用于数组

    程序1: //2022年9月20日22:06:27 #include <iostream> #pragma warning(disable:4996) using namespace st ...

  6. java方法参数(超详细)

    前言 在上一篇文章中,壹哥给大家讲解了方法的定义.调用和返回值,但方法的内容还有很多,比如方法的参数是怎么回事?接下来壹哥会在这篇文章中,继续给大家讲解方法参数相关的知识,这就是我们今天要学习的内容. ...

  7. 因为手哆嗦,发现了一个关于Python逗号的隐藏用法

    python常规的用法,众多pythoner早已​熟烂于心,如: 1.当一个元组只有一个元素时 a = (1, ) 2.当表示解包一个容器时 a = [('amo', 1), ('bmo', 1)] ...

  8. rocketmq-spring : 实战与源码解析一网打尽

    RocketMQ 是大家耳熟能详的消息队列,开源项目 rocketmq-spring 可以帮助开发者在 Spring Boot 项目中快速整合 RocketMQ. 这篇文章会介绍 Spring Boo ...

  9. python中socket使用UDP协议简单实现服务端与客户端通信

    UDP为不可靠传输,也就是发送方不关心对方是否收到消息,一般用于聊天软件.但现在的聊天软件虽然使用的是UDP协议,但已从代码层面上解决了丢失信息的问题. 下面使用python代码简单实现了服务端与客户 ...

  10. kubernetes(k8s)常用deploy模板 并验证

    kubernetes常用deploy模板,并验证 编写deploy配置文件 root@hello:~# cat deploy.yaml  apiVersion: apps/v1 kind: Deplo ...