【Azure 存儲(chǔ)服務(wù)】Java Storage SDK 調(diào)用 uploadWithResponse 代碼示例(詢問(wèn)ChatGTP得代碼原型后人力驗(yàn)證)

問(wèn)題描述

查看Java Storage SDK,想找一個(gè) uploadWithResponse 的示例代碼踢京,但是通過(guò)全網(wǎng)搜索翔试,結(jié)果沒(méi)有任何有幫助的代碼爆土。使用最近ChatGPT來(lái)尋求答案鹉究,得到非常有格式的內(nèi)容:

| 問(wèn):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.

繼續(xù)追問(wèn):BlobParallelUploadOptions with string content

答:(前面4步的答案一樣宇立,第五步的內(nèi)容為)

  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 

*在繼續(xù)問(wèn):如何使用 “To avoid overwriting, pass "" to setIfNoneMatch(String ifNoneMatch). how to use the setifnonematch” **
| 答:(前面4步的答案一樣,第五步的內(nèi)容為)

  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.

問(wèn)題解答

根據(jù)ChatGPT提供的三部分信息自赔,通過(guò)人力拼接和測(cè)試妈嘹。得到了成功執(zhí)行的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);

                //準(zhǔn)備content并且初始化blob parallel upload options對(duì)象
               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........");                        
                }

說(shuō)明:

1) content 中為需要寫(xiě)入Blob的內(nèi)容

2) 把string轉(zhuǎn)換為以UTF_8編碼的input stream

3) 根據(jù) input stream來(lái)初始化 blob paralle upload options對(duì)象

4) 設(shè)置 Request Conditions,當(dāng)不需要重寫(xiě)的時(shí)候匿级,可以使用 setIfNoneMatch("*")蟋滴。如果操作的文件存在染厅,則會(huì)出現(xiàn) Status code 409, BlobAlreadyExistss 提示痘绎。

5) 調(diào)用upload with response方法,獲取返回值肖粮,如果 返回值得status code為 201孤页,表示Storage Account接受了這次 blob 內(nèi)容的改動(dòng)。

運(yùn)行效果展示圖

blob upload with response.gif

參考資料

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 存儲(chǔ)客戶端庫(kù) : 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

當(dāng)在復(fù)雜的環(huán)境中面臨問(wèn)題涩馆,格物之道需:濁而靜之徐清行施,安以動(dòng)之徐生允坚。 云中,恰是如此!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蛾号,一起剝皮案震驚了整個(gè)濱河市稠项,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鲜结,老刑警劉巖展运,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異精刷,居然都是意外死亡拗胜,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)怒允,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)埂软,“玉大人,你說(shuō)我怎么就攤上這事纫事】迸希” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵儿礼,是天一觀的道長(zhǎng)咖杂。 經(jīng)常有香客問(wèn)我,道長(zhǎng)蚊夫,這世上最難降的妖魔是什么诉字? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮知纷,結(jié)果婚禮上壤圃,老公的妹妹穿的比我還像新娘。我一直安慰自己琅轧,他們只是感情好伍绳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著乍桂,像睡著了一般冲杀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上睹酌,一...
    開(kāi)封第一講書(shū)人閱讀 51,754評(píng)論 1 307
  • 那天权谁,我揣著相機(jī)與錄音,去河邊找鬼憋沿。 笑死旺芽,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播采章,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼运嗜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了悯舟?” 一聲冷哼從身側(cè)響起担租,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎抵怎,沒(méi)想到半個(gè)月后翩活,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡便贵,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年菠镇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片承璃。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡利耍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出盔粹,到底是詐尸還是另有隱情隘梨,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布舷嗡,位于F島的核電站轴猎,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏进萄。R本人自食惡果不足惜捻脖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望中鼠。 院中可真熱鬧可婶,春花似錦、人聲如沸援雇。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)惫搏。三九已至具温,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間筐赔,已是汗流浹背铣猩。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留川陆,地道東北人剂习。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像较沪,于是被迫代替她去往敵國(guó)和親鳞绕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容