Skip to content

Commit 0b188e4

Browse files
committed
understand the sample code
Signed-off-by: Jianguo Ma <[email protected]>
1 parent 784be41 commit 0b188e4

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

articles/storage/blobs/storage-quickstart-blobs-java-quarkus.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,107 @@ mvn package -Dnative -Dquarkus.native.container-build
130130

131131
Next, you walk through the sample code to understand how it works.
132132

133+
### Inject a client object with authorized access
134+
135+
Working with any Azure resource using the SDK begins with creating a client object. The Quarkus extension for Azure Blob Storage automatically injects a client object with authorized access using `DefaultAzureCredential`.
136+
137+
To successfully inject a client object, first you need to add the extension `quarkus-azure-storage-blob` to your `pom.xml` file as a dependency:
138+
139+
```xml
140+
<dependency>
141+
<groupId>io.quarkiverse.azureservices</groupId>
142+
<artifactId>quarkus-azure-storage-blob</artifactId>
143+
<version>${quarkus-azure-storage-blob.version}</version>
144+
</dependency>
145+
```
146+
147+
Next, you can inject the client object into your application code using the `@Inject` annotation:
148+
149+
```java
150+
@Inject
151+
BlobServiceClient blobServiceClient;
152+
```
153+
154+
This is all you need to code to get a client object using the Quarkus extension for Azure Blob Storage. To make sure the client object is authorized to access your storage account at runtime, you need to follow steps in the previous section [Authenticate to Azure and authorize access to blob data](#authenticate-to-azure-and-authorize-access-to-blob-data) before running the application.
155+
156+
### Manage blobs and containers
157+
158+
The following code snippet shows how to create a container, upload a blob, list blobs in a container, and download a blob:
159+
160+
```java
161+
// Create a unique name for the container
162+
String containerName = "quickstartblobs" + java.util.UUID.randomUUID();
163+
164+
// Create the container and return a container client object
165+
BlobContainerClient blobContainerClient = blobServiceClient.createBlobContainer(containerName);
166+
167+
// Create the ./data/ directory and a file for uploading and downloading
168+
String localPath = "./data/";
169+
new File(localPath).mkdirs();
170+
String fileName = "quickstart" + java.util.UUID.randomUUID() + ".txt";
171+
172+
// Get a reference to a blob
173+
BlobClient blobClient = blobContainerClient.getBlobClient(fileName);
174+
175+
// Write text to the file
176+
FileWriter writer = null;
177+
try
178+
{
179+
writer = new FileWriter(localPath + fileName, true);
180+
writer.write("Hello, World!");
181+
writer.close();
182+
}
183+
catch (IOException ex)
184+
{
185+
System.out.println(ex.getMessage());
186+
}
187+
188+
System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl());
189+
190+
// Upload the blob
191+
blobClient.uploadFromFile(localPath + fileName);
192+
193+
System.out.println("\nListing blobs...");
194+
195+
// List the blob(s) in the container.
196+
for (BlobItem blobItem : blobContainerClient.listBlobs()) {
197+
System.out.println("\t" + blobItem.getName());
198+
}
199+
200+
// Download the blob to a local file
201+
202+
// Append the string "DOWNLOAD" before the .txt extension for comparison purposes
203+
String downloadFileName = fileName.replace(".txt", "DOWNLOAD.txt");
204+
205+
System.out.println("\nDownloading blob to\n\t " + localPath + downloadFileName);
206+
207+
blobClient.downloadToFile(localPath + downloadFileName);
208+
209+
File downloadedFile = new File(localPath + downloadFileName);
210+
File localFile = new File(localPath + fileName);
211+
212+
// Clean up resources
213+
System.out.println("\nPress the Enter key to begin clean up");
214+
System.console().readLine();
215+
216+
System.out.println("Deleting blob container...");
217+
blobContainerClient.delete();
218+
219+
System.out.println("Deleting the local source and downloaded files...");
220+
localFile.delete();
221+
downloadedFile.delete();
222+
223+
System.out.println("Done");
224+
```
225+
226+
These operations are similar to the [Quickstart: Azure Blob Storage client library for Java](storage-quickstart-blobs-java.md). For more detailed code explanations, see the following sections in that quickstart:
227+
228+
- [Create a container](storage-quickstart-blobs-java.md#create-a-container)
229+
- [Upload blobs to a container](storage-quickstart-blobs-java.md#upload-blobs-to-a-container)
230+
- [List blobs in a container](storage-quickstart-blobs-java.md#list-blobs-in-a-container)
231+
- [Download blobs](storage-quickstart-blobs-java.md#download-blobs)
232+
- [Delete a container](storage-quickstart-blobs-java.md#delete-a-container)
233+
133234
## Next step
134235

135236
> [!div class="nextstepaction"]

0 commit comments

Comments
 (0)