激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 【Azure 存儲服務】Java Storage SDK 調用 uploadWithResponse 代碼示例(詢問ChatGTP得代碼原型后人力驗證)

【Azure 存儲服務】Java Storage SDK 調用 uploadWithResponse 代碼示例(詢問ChatGTP得代碼原型后人力驗證)

2023-05-09 01:01未知服務器之家 Java教程

問題描述 查看Java Storage SDK,想找一個?uploadWithResponse? 的示例代碼,但是通過全網搜索,結果沒有任何有幫助的代碼。使用最近ChatGPT來尋求答案,得到非常有格式的內容: 問:java azure storage account to use uploadWithResponse 答:To use

問題描述

查看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 內容的改動。

?

運行效果展示圖

【Azure 存儲服務】Java Storage SDK 調用 uploadWithResponse 代碼示例(詢問ChatGTP得代碼原型后人力驗證)

?

?

參考資料

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

?

?

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精国品产一区二区三区有限公司 | 色网在线视频 | 日本久久综合网 | 久久国产亚洲视频 | 日韩激情一区 | 精品国产一区二区三区在线观看 | 特黄一区二区三区 | 91一级毛片 | 久久精品成人免费国产片桃视频 | 精品中文字幕久久久久四十五十骆 | 免费欧美一级视频 | 久久这里只有精品1 | 精品国产一区二区三区久久久蜜月 | h视频免费在线观看 | 一级毛片在线看 | 女人解衣喂奶电影 | 黄色大片在线免费看 | 国产伦乱视频 | 国产一区精品在线观看 | 石原莉奈日韩一区二区三区 | 99影视在线视频免费观看 | 在线播放一区二区三区 | 久久久亚洲高清 | 国内精品久久久久久久久久久久 | 欧美中文日韩 | 欧美一级高潮 | 日韩视频在线观看免费视频 | 日韩欧美电影一区二区三区 | 欧美 日韩 亚洲 中文 | 国产精品v片在线观看不卡 成人一区二区三区在线 | 电影一级毛片 | 日日摸夜夜添夜夜添牛牛 | 国产一级淫| 日本精品黄色 | 欧美精品一区二区视频 | 一区二区国产在线 | 国产精品视频中文字幕 | 97中文字幕第一一一页 | 免费a级作爱片免费观看欧洲 | 免费男女乱淫真视频 | 免费一级在线观看 |