Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 45 additions & 26 deletions sdk/storage/azure-storage-blob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,51 +124,57 @@ The following sections provide several code snippets covering some of the most c

Create a `BlobServiceClient` using the [`sasToken`](#get-credentials) generated above.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L27-L30 -->
```java
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.buildClient();
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.buildClient();
```

or

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L34-L36 -->
```java
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("<your-storage-account-url>" + "?" + "<your-sasToken>")
.buildClient();
.endpoint("<your-storage-account-url>" + "?" + "<your-sasToken>")
.buildClient();
```

### Create a `BlobContainerClient`

Create a `BlobContainerClient` using a `BlobServiceClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L40-L40 -->
```java
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mycontainer");
```

Create a `BlobContainerClient` from the builder [`sasToken`](#get-credentials) generated above.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L44-L48 -->
```java
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.containerName("mycontainer")
.buildClient();
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.containerName("mycontainer")
.buildClient();
```

or

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L52-L54 -->
```java
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.endpoint("<your-storage-account-url>" + "/" + "mycontainer" + "?" + "<your-sasToken>")
.buildClient();
.endpoint("<your-storage-account-url>" + "/" + "mycontainer" + "?" + "<your-sasToken>")
.buildClient();
```

### Create a `BlobClient`

Create a `BlobClient` using a `BlobContainerClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L58-L58 -->
```java
BlobClient blobClient = blobContainerClient.getBlobClient("myblob");
```
Expand All @@ -177,27 +183,30 @@ or

Create a `BlobClient` from the builder [`sasToken`](#get-credentials) generated above.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L62-L67 -->
```java
BlobClient blobClient = new BlobClientBuilder()
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.containerName("mycontainer")
.blobName("myblob")
.buildClient();
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.containerName("mycontainer")
.blobName("myblob")
.buildClient();
```

or

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L71-L73 -->
```java
BlobClient blobClient = new BlobClientBuilder()
.endpoint("<your-storage-account-url>" + "/" + "mycontainer" + "/" + "myblob" +"?" + "<your-sasToken>")
.buildClient();
.endpoint("<your-storage-account-url>" + "/" + "mycontainer" + "/" + "myblob" + "?" + "<your-sasToken>")
.buildClient();
```

### Create a container

Create a container using a `BlobServiceClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L77-L77 -->
```java
blobServiceClient.createBlobContainer("mycontainer");
```
Expand All @@ -206,6 +215,7 @@ or

Create a container using a `BlobContainerClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L81-L81 -->
```java
blobContainerClient.create();
```
Expand All @@ -214,18 +224,22 @@ blobContainerClient.create();

Upload from an `InputStream` to a blob using a `BlockBlobClient` generated from a `BlobContainerClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L85-L91 -->
```java
BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient("myblockblob").getBlockBlobClient();
String dataSample = "samples";
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(dataSample.getBytes())) {
blockBlobClient.upload(dataStream, dataSample.length());
} catch (IOException e) {
e.printStackTrace();
}
```

### Upload a blob from local path

Upload a file to a blob using a `BlobClient` generated from a `BlobContainerClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L95-L96 -->
```java
BlobClient blobClient = blobContainerClient.getBlobClient("myblockblob");
blobClient.uploadFromFile("local-file.jpg");
Expand All @@ -235,16 +249,20 @@ blobClient.uploadFromFile("local-file.jpg");

Download a blob to an `OutputStream` using a `BlobClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L100-L104 -->
```java
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
blobClient.download(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
```

### Download a blob to local path

Download blob to a local file using a `BlobClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L108-L108 -->
```java
blobClient.downloadToFile("downloaded-file.jpg");
```
Expand All @@ -253,22 +271,23 @@ blobClient.downloadToFile("downloaded-file.jpg");

Enumerating all blobs using a `BlobContainerClient`.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L112-L114 -->
```java
Iterator<BlobItem> it = blobContainerClient.listBlobs().iterator();
it.forEachRemaining(
{ blobItem -> System.out.println("This is the blob name: " + blobItem.getName()) }
);
for (BlobItem blobItem : blobContainerClient.listBlobs()) {
System.out.println("This is the blob name: " + blobItem.getName());
}
```

### Authenticate with Azure Identity

The [Azure Identity library][identity] provides Azure Active Directory support for authenticating with Azure Storage.

<!-- embedme ./src/samples/java/com/azure/storage/blob/ReadmeSamples.java#L118-L121 -->
```java
BlobServiceClient blobStorageClient = new BlobServiceClientBuilder()
.endpoint(endpoint)
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
.endpoint("<your-storage-account-url>")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
```

## Troubleshooting
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.storage.blob;

import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.specialized.BlockBlobClient;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
* WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS
* ARE USED TO EXTRACT APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING
* LINE NUMBERS OF EXISTING CODE SAMPLES.
*
* Code samples for the README.md
*/
public class ReadmeSamples {

private BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().buildClient();
private BlobContainerClient blobContainerClient = new BlobContainerClientBuilder().buildClient();
private BlobClient blobClient = new BlobClientBuilder().buildClient();

public void getBlobServiceClient1() {
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.buildClient();
}

public void getBlobServiceClient2() {
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("<your-storage-account-url>" + "?" + "<your-sasToken>")
.buildClient();
}

public void getBlobContainerClient1() {
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mycontainer");
}

public void getBlobContainerClient2() {
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.containerName("mycontainer")
.buildClient();
}

public void getBlobContainerClient3() {
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.endpoint("<your-storage-account-url>" + "/" + "mycontainer" + "?" + "<your-sasToken>")
.buildClient();
}

public void getBlobClient1() {
BlobClient blobClient = blobContainerClient.getBlobClient("myblob");
}

public void getBlobClient2() {
BlobClient blobClient = new BlobClientBuilder()
.endpoint("<your-storage-account-url>")
.sasToken("<your-sasToken>")
.containerName("mycontainer")
.blobName("myblob")
.buildClient();
}

public void getBlobClient3() {
BlobClient blobClient = new BlobClientBuilder()
.endpoint("<your-storage-account-url>" + "/" + "mycontainer" + "/" + "myblob" + "?" + "<your-sasToken>")
.buildClient();
}

public void createBlobContainerClient1() {
blobServiceClient.createBlobContainer("mycontainer");
}

public void createBlobContainerClient2() {
blobContainerClient.create();
}

public void uploadBlobFromStream() {
BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient("myblockblob").getBlockBlobClient();
String dataSample = "samples";
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(dataSample.getBytes())) {
blockBlobClient.upload(dataStream, dataSample.length());
} catch (IOException e) {
e.printStackTrace();
}
}

public void uploadBlobFromFile() {
BlobClient blobClient = blobContainerClient.getBlobClient("myblockblob");
blobClient.uploadFromFile("local-file.jpg");
}

public void downloadBlobToStream() {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
blobClient.download(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}

public void downloadBlobToFile() {
blobClient.downloadToFile("downloaded-file.jpg");
}

public void enumerateBlobs() {
for (BlobItem blobItem : blobContainerClient.listBlobs()) {
System.out.println("This is the blob name: " + blobItem.getName());
}
}

public void authWithIdentity() {
BlobServiceClient blobStorageClient = new BlobServiceClientBuilder()
.endpoint("<your-storage-account-url>")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
}

}

Loading