diff --git a/sdk/storage/azure-storage-blob/README.md b/sdk/storage/azure-storage-blob/README.md index 5a23100d1ff0..2d76eaaf1353 100644 --- a/sdk/storage/azure-storage-blob/README.md +++ b/sdk/storage/azure-storage-blob/README.md @@ -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. + ```java BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() - .endpoint("") - .sasToken("") - .buildClient(); + .endpoint("") + .sasToken("") + .buildClient(); ``` or + ```java BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() - .endpoint("" + "?" + "") - .buildClient(); + .endpoint("" + "?" + "") + .buildClient(); ``` ### Create a `BlobContainerClient` Create a `BlobContainerClient` using a `BlobServiceClient`. + ```java BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mycontainer"); ``` Create a `BlobContainerClient` from the builder [`sasToken`](#get-credentials) generated above. + ```java BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() - .endpoint("") - .sasToken("") - .containerName("mycontainer") - .buildClient(); + .endpoint("") + .sasToken("") + .containerName("mycontainer") + .buildClient(); ``` or + ```java BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() - .endpoint("" + "/" + "mycontainer" + "?" + "") - .buildClient(); + .endpoint("" + "/" + "mycontainer" + "?" + "") + .buildClient(); ``` ### Create a `BlobClient` Create a `BlobClient` using a `BlobContainerClient`. + ```java BlobClient blobClient = blobContainerClient.getBlobClient("myblob"); ``` @@ -177,27 +183,30 @@ or Create a `BlobClient` from the builder [`sasToken`](#get-credentials) generated above. + ```java BlobClient blobClient = new BlobClientBuilder() - .endpoint("") - .sasToken("") - .containerName("mycontainer") - .blobName("myblob") - .buildClient(); + .endpoint("") + .sasToken("") + .containerName("mycontainer") + .blobName("myblob") + .buildClient(); ``` or + ```java BlobClient blobClient = new BlobClientBuilder() - .endpoint("" + "/" + "mycontainer" + "/" + "myblob" +"?" + "") - .buildClient(); + .endpoint("" + "/" + "mycontainer" + "/" + "myblob" + "?" + "") + .buildClient(); ``` ### Create a container Create a container using a `BlobServiceClient`. + ```java blobServiceClient.createBlobContainer("mycontainer"); ``` @@ -206,6 +215,7 @@ or Create a container using a `BlobContainerClient`. + ```java blobContainerClient.create(); ``` @@ -214,11 +224,14 @@ blobContainerClient.create(); Upload from an `InputStream` to a blob using a `BlockBlobClient` generated from a `BlobContainerClient`. + ```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(); } ``` @@ -226,6 +239,7 @@ try (ByteArrayInputStream dataStream = new ByteArrayInputStream(dataSample.getBy Upload a file to a blob using a `BlobClient` generated from a `BlobContainerClient`. + ```java BlobClient blobClient = blobContainerClient.getBlobClient("myblockblob"); blobClient.uploadFromFile("local-file.jpg"); @@ -235,9 +249,12 @@ blobClient.uploadFromFile("local-file.jpg"); Download a blob to an `OutputStream` using a `BlobClient`. + ```java -try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { +try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { blobClient.download(outputStream); +} catch (IOException e) { + e.printStackTrace(); } ``` @@ -245,6 +262,7 @@ try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { Download blob to a local file using a `BlobClient`. + ```java blobClient.downloadToFile("downloaded-file.jpg"); ``` @@ -253,22 +271,23 @@ blobClient.downloadToFile("downloaded-file.jpg"); Enumerating all blobs using a `BlobContainerClient`. + ```java -Iterator 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. + ```java BlobServiceClient blobStorageClient = new BlobServiceClientBuilder() - .endpoint(endpoint) - .credential(new DefaultAzureCredentialBuilder().build()) - .buildClient(); + .endpoint("") + .credential(new DefaultAzureCredentialBuilder().build()) + .buildClient(); ``` ## Troubleshooting diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/ReadmeSamples.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/ReadmeSamples.java new file mode 100644 index 000000000000..003e72f8e6a3 --- /dev/null +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/ReadmeSamples.java @@ -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("") + .sasToken("") + .buildClient(); + } + + public void getBlobServiceClient2() { + BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() + .endpoint("" + "?" + "") + .buildClient(); + } + + public void getBlobContainerClient1() { + BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mycontainer"); + } + + public void getBlobContainerClient2() { + BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() + .endpoint("") + .sasToken("") + .containerName("mycontainer") + .buildClient(); + } + + public void getBlobContainerClient3() { + BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() + .endpoint("" + "/" + "mycontainer" + "?" + "") + .buildClient(); + } + + public void getBlobClient1() { + BlobClient blobClient = blobContainerClient.getBlobClient("myblob"); + } + + public void getBlobClient2() { + BlobClient blobClient = new BlobClientBuilder() + .endpoint("") + .sasToken("") + .containerName("mycontainer") + .blobName("myblob") + .buildClient(); + } + + public void getBlobClient3() { + BlobClient blobClient = new BlobClientBuilder() + .endpoint("" + "/" + "mycontainer" + "/" + "myblob" + "?" + "") + .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("") + .credential(new DefaultAzureCredentialBuilder().build()) + .buildClient(); + } + +} + diff --git a/sdk/storage/azure-storage-file-datalake/README.md b/sdk/storage/azure-storage-file-datalake/README.md index 71da4b117273..fb91fbf7da17 100644 --- a/sdk/storage/azure-storage-file-datalake/README.md +++ b/sdk/storage/azure-storage-file-datalake/README.md @@ -148,25 +148,28 @@ The following sections provide several code snippets covering some of the most c Create a `DataLakeServiceClient` using the [`sasToken`](#get-credentials) generated above. + ```java DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder() - .endpoint("") - .sasToken("") - .buildClient(); + .endpoint("") + .sasToken("") + .buildClient(); ``` or + ```java DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder() - .endpoint("" + "?" + "") - .buildClient(); + .endpoint("" + "?" + "") + .buildClient(); ``` ### Create a `DataLakeFileSystemClient` Create a `DataLakeFileSystemClient` using a `DataLakeServiceClient`. + ```java DataLakeFileSystemClient dataLakeFileSystemClient = dataLakeServiceClient.getFileSystemClient("myfilesystem"); ``` @@ -175,26 +178,29 @@ or Create a `DataLakeFileSystemClient` from the builder [`sasToken`](#get-credentials) generated above. + ```java DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder() - .endpoint("") - .sasToken("") - .fileSystemName("myfilesystem") - .buildClient(); + .endpoint("") + .sasToken("") + .fileSystemName("myfilesystem") + .buildClient(); ``` or + ```java DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder() - .endpoint("" + "/" + "myfilesystem" + "?" + "") - .buildClient(); + .endpoint("" + "/" + "myfilesystem" + "?" + "") + .buildClient(); ``` ### Create a `DataLakeFileClient` Create a `DataLakeFileClient` using a `DataLakeFileSystemClient`. + ```java DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient("myfile"); ``` @@ -203,27 +209,30 @@ or Create a `FileClient` from the builder [`sasToken`](#get-credentials) generated above. + ```java DataLakeFileClient fileClient = new DataLakePathClientBuilder() - .endpoint("") - .sasToken("") - .fileSystemName("myfilesystem") - .pathName("myfile") - .buildClient(); + .endpoint("") + .sasToken("") + .fileSystemName("myfilesystem") + .pathName("myfile") + .buildFileClient(); ``` or + ```java DataLakeFileClient fileClient = new DataLakePathClientBuilder() - .endpoint("" + "/" + "myfilesystem" + "/" + "myfile" +"?" + "") - .buildClient(); + .endpoint("" + "/" + "myfilesystem" + "/" + "myfile" + "?" + "") + .buildFileClient(); ``` ### Create a `DataLakeDirectoryClient` Get a `DataLakeDirectoryClient` using a `DataLakeFileSystemClient`. + ```java DataLakeDirectoryClient directoryClient = dataLakeFileSystemClient.getDirectoryClient("mydir"); ``` @@ -232,27 +241,30 @@ or Create a `DirectoryClient` from the builder [`sasToken`](#get-credentials) generated above. + ```java DataLakeDirectoryClient directoryClient = new DataLakePathClientBuilder() - .endpoint("") - .sasToken("") - .fileSystemName("myfilesystem") - .pathName("mydir") - .buildClient(); + .endpoint("") + .sasToken("") + .fileSystemName("myfilesystem") + .pathName("mydir") + .buildDirectoryClient(); ``` or + ```java -DataLakeFileClient fileClient = new DataLakePathClientBuilder() - .endpoint("" + "/" + "myfilesystem" + "/" + "mydir" +"?" + "") - .buildClient(); +DataLakeDirectoryClient directoryClient = new DataLakePathClientBuilder() + .endpoint("" + "/" + "myfilesystem" + "/" + "mydir" + "?" + "") + .buildDirectoryClient(); ``` ### Create a file system Create a file system using a `DataLakeServiceClient`. + ```java dataLakeServiceClient.createFileSystem("myfilesystem"); ``` @@ -261,6 +273,7 @@ or Create a file system using a `DataLakeFileSystemClient`. + ```java dataLakeFileSystemClient.create(); ``` @@ -269,37 +282,40 @@ dataLakeFileSystemClient.create(); Enumerating all paths using a `DataLakeFileSystemClient`. + ```java -Iterator it = dataLakeFileSystemClient.listPaths().iterator(); -it.forEachRemaining( - { pathItem -> System.out.println("This is the path name: " + pathItem.getName()) } - ); +for (PathItem pathItem : dataLakeFileSystemClient.listPaths()) { + System.out.println("This is the path name: " + pathItem.getName()); +} ``` ### Rename a file Rename a file using a `DataLakeFileClient`. + ```java DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient("myfile"); fileClient.create(); -fileClient.rename("new-file-name") +fileClient.rename("new-file-system-name", "new-file-name"); ``` ### Rename a directory Rename a directory using a `DataLakeDirectoryClient`. + ```java DataLakeDirectoryClient directoryClient = dataLakeFileSystemClient.getDirectoryClient("mydir"); directoryClient.create(); -directoryClient.rename("new-directory-name") +directoryClient.rename("new-file-system-name", "new-directory-name"); ``` ### Get file properties Get properties from a file using a `DataLakeFileClient`. + ```java DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient("myfile"); fileClient.create(); @@ -310,6 +326,7 @@ PathProperties properties = fileClient.getProperties(); Get properties from a directory using a `DataLakeDirectoryClient`. + ```java DataLakeDirectoryClient directoryClient = dataLakeFileSystemClient.getDirectoryClient("mydir"); directoryClient.create(); @@ -320,11 +337,12 @@ PathProperties properties = directoryClient.getProperties(); The [Azure Identity library][identity] provides Azure Active Directory support for authenticating with Azure Storage. + ```java DataLakeServiceClient storageClient = new DataLakeServiceClientBuilder() - .endpoint(endpoint) - .credential(new DefaultAzureCredentialBuilder().build()) - .buildClient(); + .endpoint("") + .credential(new DefaultAzureCredentialBuilder().build()) + .buildClient(); ``` ## Troubleshooting diff --git a/sdk/storage/azure-storage-file-datalake/src/samples/java/com/azure/storage/file/datalake/ReadmeSamples.java b/sdk/storage/azure-storage-file-datalake/src/samples/java/com/azure/storage/file/datalake/ReadmeSamples.java new file mode 100644 index 000000000000..6990f330d2d7 --- /dev/null +++ b/sdk/storage/azure-storage-file-datalake/src/samples/java/com/azure/storage/file/datalake/ReadmeSamples.java @@ -0,0 +1,139 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.storage.file.datalake; + +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.storage.file.datalake.models.PathItem; +import com.azure.storage.file.datalake.models.PathProperties; + +/** + * 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 DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder().buildClient(); + private DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder().buildClient(); + private DataLakeFileClient dataLakeFileClient = new DataLakePathClientBuilder().buildFileClient(); + private DataLakeDirectoryClient dataLakeDirectoryClient = new DataLakePathClientBuilder().buildDirectoryClient(); + + public void getDataLakeServiceClient1() { + DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder() + .endpoint("") + .sasToken("") + .buildClient(); + } + + public void getDataLakeServiceClient2() { + DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder() + .endpoint("" + "?" + "") + .buildClient(); + } + + public void getDataLakeFileSystemClient1() { + DataLakeFileSystemClient dataLakeFileSystemClient = dataLakeServiceClient.getFileSystemClient("myfilesystem"); + } + + public void getDataLakeFileSystemClient2() { + DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder() + .endpoint("") + .sasToken("") + .fileSystemName("myfilesystem") + .buildClient(); + } + + public void getDataLakeFileSystemClient3() { + DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder() + .endpoint("" + "/" + "myfilesystem" + "?" + "") + .buildClient(); + } + + public void getFileClient1() { + DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient("myfile"); + } + + public void getFileClient2() { + DataLakeFileClient fileClient = new DataLakePathClientBuilder() + .endpoint("") + .sasToken("") + .fileSystemName("myfilesystem") + .pathName("myfile") + .buildFileClient(); + } + + public void getFileClient3() { + DataLakeFileClient fileClient = new DataLakePathClientBuilder() + .endpoint("" + "/" + "myfilesystem" + "/" + "myfile" + "?" + "") + .buildFileClient(); + } + + public void getDirClient1() { + DataLakeDirectoryClient directoryClient = dataLakeFileSystemClient.getDirectoryClient("mydir"); + } + + public void getDirClient2() { + DataLakeDirectoryClient directoryClient = new DataLakePathClientBuilder() + .endpoint("") + .sasToken("") + .fileSystemName("myfilesystem") + .pathName("mydir") + .buildDirectoryClient(); + } + + public void getDirClient3() { + DataLakeDirectoryClient directoryClient = new DataLakePathClientBuilder() + .endpoint("" + "/" + "myfilesystem" + "/" + "mydir" + "?" + "") + .buildDirectoryClient(); + } + + public void createDataLakeFileSystemClient1() { + dataLakeServiceClient.createFileSystem("myfilesystem"); + } + + public void createDataLakeFileSystemClient2() { + dataLakeFileSystemClient.create(); + } + + public void enumeratePaths() { + for (PathItem pathItem : dataLakeFileSystemClient.listPaths()) { + System.out.println("This is the path name: " + pathItem.getName()); + } + } + + public void renameFile() { + DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient("myfile"); + fileClient.create(); + fileClient.rename("new-file-system-name", "new-file-name"); + } + + public void renameDirectory() { + DataLakeDirectoryClient directoryClient = dataLakeFileSystemClient.getDirectoryClient("mydir"); + directoryClient.create(); + directoryClient.rename("new-file-system-name", "new-directory-name"); + } + + public void getPropertiesFile() { + DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient("myfile"); + fileClient.create(); + PathProperties properties = fileClient.getProperties(); + } + + public void getPropertiesDirectory() { + DataLakeDirectoryClient directoryClient = dataLakeFileSystemClient.getDirectoryClient("mydir"); + directoryClient.create(); + PathProperties properties = directoryClient.getProperties(); + } + + public void authWithIdentity() { + DataLakeServiceClient storageClient = new DataLakeServiceClientBuilder() + .endpoint("") + .credential(new DefaultAzureCredentialBuilder().build()) + .buildClient(); + } + +} +