diff --git a/eng/jacoco-test-coverage/pom.xml b/eng/jacoco-test-coverage/pom.xml index 7c7e9032bb48..015665b9897b 100644 --- a/eng/jacoco-test-coverage/pom.xml +++ b/eng/jacoco-test-coverage/pom.xml @@ -32,6 +32,7 @@ 1.0.0-preview.3 4.0.0-preview.3 5.0.0-preview.3 + 12.0.0-preview.3 12.0.0-preview.3 12.0.0-preview.3 12.0.0-preview.3 @@ -96,6 +97,11 @@ azure-messaging-eventhubs ${azure-messaging-eventhubs.version} + + com.azure + azure-storage-common + ${azure-storage-common.version} + com.azure azure-storage-blob diff --git a/eng/spotbugs-aggregate-report/pom.xml b/eng/spotbugs-aggregate-report/pom.xml index c6aa0fa9356e..a092047e0e67 100644 --- a/eng/spotbugs-aggregate-report/pom.xml +++ b/eng/spotbugs-aggregate-report/pom.xml @@ -27,6 +27,7 @@ 1.0.0-preview.3 4.0.0-preview.3 5.0.0-preview.3 + 12.0.0-preview.3 12.0.0-preview.3 12.0.0-preview.3 12.0.0-preview.3 @@ -68,12 +69,13 @@ - - - - - - + ..\..\sdk\storage\azure-storage-common\src\main\java + ..\..\sdk\storage\azure-storage-blob\src\main\java + ..\..\sdk\storage\azure-storage-blob\src\samples\java + ..\..\sdk\storage\azure-storage-file\src\main\java + ..\..\sdk\storage\azure-storage-file\src\samples\java + ..\..\sdk\storage\azure-storage-queue\src\main\java + ..\..\sdk\storage\azure-storage-queue\src\samples\java @@ -185,6 +187,11 @@ azure-keyvault-secrets ${azure-keyvault.version} + + com.azure + azure-storage-common + ${azure-storage-common.version} + com.azure azure-storage-blob diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BasicExample.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BasicExample.java index da2b80dfa0a1..b8292104ffa5 100644 --- a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BasicExample.java +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BasicExample.java @@ -9,8 +9,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Locale; /** @@ -72,7 +72,7 @@ public static void main(String[] args) throws IOException { BlockBlobClient blobClient = containerClient.getBlockBlobClient("HelloWorld.txt"); String data = "Hello world!"; - InputStream dataStream = new ByteArrayInputStream(data.getBytes()); + InputStream dataStream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); /* * Create the blob with string (plain text) content. @@ -85,14 +85,14 @@ public static void main(String[] args) throws IOException { * Download the blob's content to output stream. */ int dataSize = (int) blobClient.getProperties().blobSize(); - OutputStream outputStream = new ByteArrayOutputStream(dataSize); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(dataSize); blobClient.download(outputStream); outputStream.close(); /* * Verify that the blob data round-tripped correctly. */ - if (!data.equals(outputStream.toString())) { + if (!data.equals(new String(outputStream.toByteArray(), StandardCharsets.UTF_8))) { throw new RuntimeException("The downloaded data does not match the uploaded data."); } diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobClientJavaDocCodeSnippets.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobClientJavaDocCodeSnippets.java index d49d6708186d..710e8fd65f53 100644 --- a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobClientJavaDocCodeSnippets.java +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobClientJavaDocCodeSnippets.java @@ -14,6 +14,7 @@ import com.azure.storage.blob.models.ModifiedAccessConditions; import com.azure.storage.blob.models.ReliableDownloadOptions; import com.azure.storage.blob.models.StorageAccountInfo; +import com.azure.storage.common.Constants; import java.io.ByteArrayOutputStream; import java.io.OutputStream; @@ -63,7 +64,7 @@ public void startCopyFromURL() { public void abortCopyFromURL() { // BEGIN: com.azure.storage.blob.BlobClient.abortCopyFromURL#String client.abortCopyFromURL(copyId); - System.out.printf("Aborted copy completed."); + System.out.println("Aborted copy completed."); // END: com.azure.storage.blob.BlobClient.abortCopyFromURL#String } @@ -82,7 +83,7 @@ public void copyFromURL() { public void download() { // BEGIN: com.azure.storage.blob.BlobClient.download#OutputStream client.download(new ByteArrayOutputStream()); - System.out.printf("Download completed."); + System.out.println("Download completed."); // END: com.azure.storage.blob.BlobClient.download#OutputStream } @@ -100,7 +101,7 @@ public void downloadToFile() { BlobRange range = new BlobRange(1024, 2048L); ReliableDownloadOptions options = new ReliableDownloadOptions().maxRetryRequests(5); - client.downloadToFile(file, range, null, options, null, false, timeout, new Context(key2, value2)); + client.downloadToFile(file, range, 4 * Constants.MB, options, null, false, timeout, new Context(key2, value2)); System.out.println("Completed download to file"); // END: com.azure.storage.blob.BlobClient.downloadToFile#String-BlobRange-Integer-ReliableDownloadOptions-BlobAccessConditions-boolean-Duration-Context } @@ -111,7 +112,7 @@ public void downloadToFile() { public void delete() { // BEGIN: com.azure.storage.blob.BlobClient.delete client.delete(); - System.out.printf("Delete completed."); + System.out.println("Delete completed."); // END: com.azure.storage.blob.BlobClient.delete } @@ -133,7 +134,7 @@ public void setHTTPHeaders() { client.setHTTPHeaders(new BlobHTTPHeaders() .blobContentLanguage("en-US") .blobContentType("binary")); - System.out.printf("Set HTTP headers completed"); + System.out.println("Set HTTP headers completed"); // END: com.azure.storage.blob.BlobClient.setHTTPHeaders#BlobHTTPHeaders } @@ -143,7 +144,7 @@ public void setHTTPHeaders() { public void setMetadata() { // BEGIN: com.azure.storage.blob.BlobClient.setMetadata#Metadata client.setMetadata(new Metadata(Collections.singletonMap("metadata", "value"))); - System.out.printf("Set metadata completed"); + System.out.println("Set metadata completed"); // END: com.azure.storage.blob.BlobClient.setMetadata#Metadata } diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/FileTransferExample.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/FileTransferExample.java index 33886cd10750..6d462368bba7 100644 --- a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/FileTransferExample.java +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/FileTransferExample.java @@ -9,9 +9,9 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.RandomAccessFile; -import java.net.URL; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -121,16 +121,17 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept } private static File createTempEmptyFile(String fileName) throws IOException { - URL folderUrl = FileTransferExample.class.getClassLoader().getResource("."); + String pathName = "./folderPath/" + LARGE_TEST_FOLDER; - File dirPath = new File(folderUrl.getPath() + LARGE_TEST_FOLDER); + File dirPath = new File(pathName); if (dirPath.exists() || dirPath.mkdir()) { - File f = new File(folderUrl.getPath() + LARGE_TEST_FOLDER + fileName); - if (!f.exists()) { - f.createNewFile(); + File f = new File(pathName + fileName); + if (f.exists() || f.createNewFile()) { + return f; + } else { + throw new RuntimeException("Failed to create the large file."); } - return f; } else { throw new RuntimeException("Failed to create the large file dir."); } @@ -164,9 +165,8 @@ private static String getFileChecksum(File file) throws IOException, NoSuchAlgor buf.clear(); b = ch.read(buf); } - ch.close(); - fis.close(); - return new String(md.digest()); + + return new String(md.digest(), StandardCharsets.UTF_8); } } diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/SetMetadataAndHTTPHeadersExample.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/SetMetadataAndHTTPHeadersExample.java index d98e96553b69..46629d4a717c 100644 --- a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/SetMetadataAndHTTPHeadersExample.java +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/SetMetadataAndHTTPHeadersExample.java @@ -11,6 +11,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Locale; @@ -77,7 +78,7 @@ public static void main(String[] args) throws IOException { * Data which will upload to block blob. */ String data = "Hello world!"; - InputStream dataStream = new ByteArrayInputStream(data.getBytes()); + InputStream dataStream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); blobClient.uploadWithResponse(dataStream, data.length(), blobHTTPHeaders, blobMetadata, null, null, null); /* diff --git a/sdk/storage/azure-storage-file/src/main/java/com/azure/storage/file/ShareClientBuilder.java b/sdk/storage/azure-storage-file/src/main/java/com/azure/storage/file/ShareClientBuilder.java index e7c68d979dc3..7528ce318e73 100644 --- a/sdk/storage/azure-storage-file/src/main/java/com/azure/storage/file/ShareClientBuilder.java +++ b/sdk/storage/azure-storage-file/src/main/java/com/azure/storage/file/ShareClientBuilder.java @@ -276,10 +276,9 @@ public ShareClientBuilder shareName(String shareName) { * * @param snapshot Identifier of the snapshot * @return the updated ShareClientBuilder object - * @throws NullPointerException If {@code snapshot} is {@code null}. */ public ShareClientBuilder snapshot(String snapshot) { - this.snapshot = Objects.requireNonNull(snapshot); + this.snapshot = snapshot; return this; } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryAsyncJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryAsyncJavaDocCodeSamples.java index 663a7df998b2..7e3550749a18 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryAsyncJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryAsyncJavaDocCodeSamples.java @@ -305,7 +305,7 @@ public void setMetadataAsync() { DirectoryAsyncClient directoryAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.directoryAsyncClient.setMetadata#map directoryAsyncClient.setMetadata(Collections.singletonMap("directory", "updatedMetadata")) - .subscribe(response -> System.out.printf("Setting the directory metadata completed.")); + .subscribe(response -> System.out.println("Setting the directory metadata completed.")); // END: com.azure.storage.file.directoryAsyncClient.setMetadata#map } @@ -316,7 +316,7 @@ public void setMetadataClear() { DirectoryAsyncClient directoryAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.directoryAsyncClient.setMetadata#map.clearMetadata directoryAsyncClient.setMetadata(null) - .doOnSuccess(response -> System.out.printf("Clearing the directory metadata compleetd")); + .doOnSuccess(response -> System.out.println("Clearing the directory metadata completed")); // END: com.azure.storage.file.directoryAsyncClient.setMetadata#map.clearMetadata } @@ -339,7 +339,7 @@ public void setMetadataWithResponse() { DirectoryAsyncClient directoryAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.directoryAsyncClient.setMetadataWithResponse#map directoryAsyncClient.setMetadataWithResponse(Collections.singletonMap("directory", "updatedMetadata")) - .subscribe(response -> System.out.printf("Setting the directory metadata completed with status code:" + .subscribe(response -> System.out.println("Setting the directory metadata completed with status code:" + response.statusCode())); // END: com.azure.storage.file.directoryAsyncClient.setMetadataWithResponse#map } @@ -363,11 +363,8 @@ public void forceCloseHandlesAsync() { DirectoryAsyncClient directoryAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.directoryAsyncClient.forceCloseHandles directoryAsyncClient.listHandles(10, true) - .subscribe(handleItem -> { - directoryAsyncClient.forceCloseHandles(handleItem.handleId(), true) - .subscribe(numOfClosedHandles -> - System.out.printf("Close %d handles.", numOfClosedHandles)); - }); + .subscribe(handleItem -> directoryAsyncClient.forceCloseHandles(handleItem.handleId(), true) + .subscribe(numOfClosedHandles -> System.out.printf("Closed %d handles.", numOfClosedHandles))); // END: com.azure.storage.file.directoryAsyncClient.forceCloseHandles } @@ -384,7 +381,8 @@ public void getShareSnapshotIdAsync() { .directoryPath("mydirectory") .snapshot(currentTime.toString()) .buildAsyncClient(); - directoryAsyncClient.getShareSnapshotId(); + + System.out.printf("Snapshot ID: %s%n", directoryAsyncClient.getShareSnapshotId()); // END: com.azure.storage.file.directoryAsyncClient.getShareSnapshotId } } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryJavaDocCodeSamples.java index 6760e866ee8d..a41dea8e8058 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectoryJavaDocCodeSamples.java @@ -360,7 +360,8 @@ public void getShareSnapshotId() { .directoryPath("mydirectory") .snapshot(currentTime.toString()) .buildClient(); - directoryClient.getShareSnapshotId(); + + System.out.printf("Snapshot ID: %s%n", directoryClient.getShareSnapshotId()); // END: com.azure.storage.file.directoryClient.getShareSnapshotId } } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectorySample.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectorySample.java index be27f01b75eb..000d0bc00f85 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectorySample.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/DirectorySample.java @@ -48,10 +48,9 @@ public static void main(String[] args) { } // Create a 1KB file under the child directory. - DirectoryClient childDirClient = null; + DirectoryClient childDirClient = directoryClient.getSubDirectoryClient(childDirectoryName); String fileName = generateRandomName(); try { - childDirClient = directoryClient.getSubDirectoryClient(childDirectoryName); childDirClient.createFile(fileName, 1024); } catch (StorageErrorException e) { System.out.println("Failed to create a file under the child directory. Reasons: " + e.getMessage()); @@ -67,11 +66,8 @@ public static void main(String[] args) { // List all the sub directories and files. try { directoryClient.listFilesAndDirectories().forEach( - fileRef -> { - System.out.printf("Is the resource a directory? %b. The resource name is: ", fileRef.isDirectory(), - fileRef.name()); - } - ); + fileRef -> System.out.printf("Is the resource a directory? %b. The resource name is: %s%n", + fileRef.isDirectory(), fileRef.name())); } catch (StorageErrorException e) { System.out.println("Failed to list all the subdirectories and files. Reasons: " + e.getMessage()); } @@ -79,7 +75,7 @@ public static void main(String[] args) { // Get the parent directory properties. try { DirectoryProperties propertiesResponse = directoryClient.getProperties(); - System.out.printf("This is the eTag %s of the directory: ", propertiesResponse.eTag()); + System.out.printf("This is the eTag of the directory: %s%n", propertiesResponse.eTag()); } catch (StorageErrorException e) { System.out.println("Failed to get the properties of the parent directory"); } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileAsyncJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileAsyncJavaDocCodeSamples.java index c2560fea3df0..19e347a7383d 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileAsyncJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileAsyncJavaDocCodeSamples.java @@ -108,7 +108,7 @@ public void createFileAsyncMaxOverload() { // BEGIN: com.azure.storage.file.fileAsyncClient.create#long-filehttpheaders-map FileHTTPHeaders httpHeaders = new FileHTTPHeaders().fileContentType("text/plain"); fileAsyncClient.create(1024) - .doOnSuccess(response -> System.out.printf("Creating the file completed.")); + .doOnSuccess(response -> System.out.println("Creating the file completed.")); // END: com.azure.storage.file.fileAsyncClient.create#long-filehttpheaders-map } @@ -162,7 +162,7 @@ public void abortCopyFileAsync() { FileAsyncClient fileAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileAsyncClient.abortCopy#string fileAsyncClient.abortCopy("someCopyId") - .doOnSuccess(response -> System.out.printf("Abort copying the file completed.")); + .doOnSuccess(response -> System.out.println("Abort copying the file completed.")); // END: com.azure.storage.file.fileAsyncClient.abortCopy#string } @@ -260,12 +260,8 @@ public void uploadFileAsyncMaxOverload() { FileAsyncClient fileAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileAsyncClient.uploadFromFile#string-filerangewritetype fileAsyncClient.uploadFromFile("someFilePath", FileRangeWriteType.UPDATE) - .subscribe(response -> { - if (fileAsyncClient.getProperties() != null) { - System.out.printf("Upload the file with length of %d completed", - fileAsyncClient.getPropertiesWithResponse().block().value().contentLength()); - } - }); + .subscribe(response -> System.out.println("Uploaded file successfully!"), + throwable -> System.err.printf("Failed to upload. Reason: %s%n", throwable.getMessage())); // END: com.azure.storage.file.fileAsyncClient.uploadFromFile#string-filerangewritetype } @@ -408,7 +404,7 @@ public void setMetadataAsync() { FileAsyncClient fileAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileAsyncClient.setMetadata#map fileAsyncClient.setMetadata(Collections.singletonMap("file", "updatedMetadata")) - .doOnSuccess(response -> System.out.printf("Setting the file metadata completed.")); + .doOnSuccess(response -> System.out.println("Setting the file metadata completed.")); // END: com.azure.storage.file.fileAsyncClient.setMetadata#map } @@ -443,7 +439,7 @@ public void clearMetadata() { FileAsyncClient fileAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileAsyncClient.setMetadata#map.clearMetadata fileAsyncClient.setMetadata(null).subscribe( - response -> System.out.printf("Setting the file metadata completed.") + response -> System.out.println("Setting the file metadata completed.") ); // END: com.azure.storage.file.fileAsyncClient.setMetadata#map.clearMetadata } @@ -456,7 +452,7 @@ public void setHTTPHeadersAsync() { // BEGIN: com.azure.storage.file.fileAsyncClient.setHttpHeaders#long-filehttpheaders FileHTTPHeaders httpHeaders = new FileHTTPHeaders().fileContentType("text/plain"); fileAsyncClient.setHttpHeaders(1024, httpHeaders) - .doOnSuccess(response -> System.out.printf("Setting the file httpHeaders completed.")); + .doOnSuccess(response -> System.out.println("Setting the file httpHeaders completed.")); // END: com.azure.storage.file.fileAsyncClient.setHttpHeaders#long-filehttpheaders } @@ -492,7 +488,7 @@ public void clearHTTPHeaders() { FileAsyncClient fileAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileAsyncClient.setHttpHeaders#long-filehttpheaders.clearHttpHeaders fileAsyncClient.setHttpHeaders(1024, null) - .subscribe(response -> System.out.printf("Setting the file httpHeaders completed.")); + .subscribe(response -> System.out.println("Setting the file httpHeaders completed.")); // END: com.azure.storage.file.fileAsyncClient.setHttpHeaders#long-filehttpheaders.clearHttpHeaders } @@ -568,7 +564,8 @@ public void getShareSnapshotIdAsync() { .filePath("myfiile") .snapshot(currentTime.toString()) .buildAsyncClient(); - fileAsyncClient.getShareSnapshotId(); + + System.out.printf("Snapshot ID: %s%n", fileAsyncClient.getShareSnapshotId()); // END: com.azure.storage.file.fileAsyncClient.getShareSnapshotId } } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileJavaDocCodeSamples.java index 852487432620..4639f16d7911 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileJavaDocCodeSamples.java @@ -461,7 +461,7 @@ public void listHandles() { FileClient fileClient = createClientWithSASToken(); // BEGIN: com.azure.storage.file.fileClient.listHandles fileClient.listHandles() - .forEach(handleItem -> System.out.printf("List handles completed with handleId %d", + .forEach(handleItem -> System.out.printf("List handles completed with handleId %s", handleItem.handleId())); // END: com.azure.storage.file.fileClient.listHandles } @@ -473,7 +473,7 @@ public void listHandlesWithOverload() { FileClient fileClient = createClientWithSASToken(); // BEGIN: com.azure.storage.file.fileClient.listHandles#integer fileClient.listHandles(10) - .forEach(handleItem -> System.out.printf("List handles completed with handleId %d", + .forEach(handleItem -> System.out.printf("List handles completed with handleId %s", handleItem.handleId())); // END: com.azure.storage.file.fileClient.listHandles#integer } @@ -505,7 +505,8 @@ public void getShareSnapshotId() { .filePath("myfile") .snapshot(currentTime.toString()) .buildClient(); - fileClient.getShareSnapshotId(); + + System.out.printf("Snapshot ID: %s%n", fileClient.getShareSnapshotId()); // END: com.azure.storage.file.fileClient.getShareSnapshotId } } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileSample.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileSample.java index 89d2003f1b64..d6ae6f81d732 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileSample.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileSample.java @@ -31,9 +31,8 @@ private static String generateRandomName() { * The main method shows how to do the base operation using file sync client. * @param args No args needed for the main method. * @throws RuntimeException If error occurs when make storage API call. - * @throws Exception If there are any IO exception occurs. */ - public static void main(String[] args) throws Exception { + public static void main(String[] args) { String shareName = generateRandomName(); ShareClient shareClient = new ShareClientBuilder().endpoint(ENDPOINT).shareName(shareName).buildClient(); shareClient.create(); @@ -67,8 +66,7 @@ public static void main(String[] args) throws Exception { destFileClient.create(1024); // Copy the file from source file to destination file. - URL clientURL = null; - clientURL = srcFileClient.getFileUrl(); + URL clientURL = srcFileClient.getFileUrl(); String sourceURL = clientURL.toString() + "/" + shareName + "/" + parentDirName + "/" + srcFileName; @@ -89,8 +87,8 @@ public static void main(String[] args) throws Exception { } // Upload a local file to the storage. - URL fileFolder = FileSample.class.getClassLoader().getResource("."); - String uploadPath = fileFolder.getPath() + "testfiles/" + "uploadSample.txt"; + String filePath = "C:/filePath/"; + String uploadPath = filePath + "testfiles/" + "uploadSample.txt"; try { srcFileClient.uploadFromFile(uploadPath); @@ -99,11 +97,11 @@ public static void main(String[] args) throws Exception { } // Download storage file to local file. - String downloadPath = fileFolder.getPath() + "testfiles/" + "downloadSample.txt"; + String downloadPath = filePath + "testfiles/" + "downloadSample.txt"; File downloadFile = new File(downloadPath); try { - if (!Files.exists(downloadFile.toPath())) { - downloadFile.createNewFile(); + if (!Files.exists(downloadFile.toPath()) && !downloadFile.createNewFile()) { + throw new RuntimeException("Failed to create new upload file."); } } catch (IOException e) { throw new RuntimeException("Failed to create new upload file."); @@ -114,8 +112,8 @@ public static void main(String[] args) throws Exception { System.out.println("Failed to download file from storage. Reasons: " + e.getMessage()); } - if (!Files.exists(downloadFile.toPath())) { - downloadFile.delete(); + if (Files.exists(downloadFile.toPath()) && !downloadFile.delete()) { + System.out.println("Failed to delete download file."); } // Get the file properties diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceAsyncJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceAsyncJavaDocCodeSamples.java index faaad03c1aea..2d21deebdf90 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceAsyncJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceAsyncJavaDocCodeSamples.java @@ -219,13 +219,13 @@ public void getPropertiesWithResponse() { public void setPropertiesAsync() { FileServiceAsyncClient fileServiceAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileServiceAsyncClient.setProperties#fileServiceProperties - FileServiceProperties properties = fileServiceAsyncClient.getPropertiesWithResponse().block().value(); + fileServiceAsyncClient.getProperties().subscribe(properties -> { + properties.minuteMetrics().enabled(true); + properties.hourMetrics().enabled(true); - properties.minuteMetrics().enabled(true); - properties.hourMetrics().enabled(true); - - fileServiceAsyncClient.setProperties(properties) - .doOnSuccess(response -> System.out.printf("Setting File service properties completed.")); + fileServiceAsyncClient.setProperties(properties) + .subscribe(r -> System.out.println("Setting File service properties completed.")); + }); // END: com.azure.storage.file.fileServiceAsyncClient.setProperties#fileServiceProperties } @@ -236,14 +236,14 @@ public void setPropertiesAsync() { public void setPropertiesWithResponseAsync() { FileServiceAsyncClient fileServiceAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileServiceAsyncClient.setPropertiesWithResponseAsync#fileServiceProperties - FileServiceProperties properties = fileServiceAsyncClient.getPropertiesWithResponse().block().value(); - - properties.minuteMetrics().enabled(true); - properties.hourMetrics().enabled(true); + fileServiceAsyncClient.getPropertiesWithResponse().subscribe(response -> { + FileServiceProperties properties = response.value(); + properties.minuteMetrics().enabled(true); + properties.hourMetrics().enabled(true); - fileServiceAsyncClient.setPropertiesWithResponse(properties) - .subscribe(response -> System.out.printf("Setting File service properties completed with status code %d", - response.statusCode())); + fileServiceAsyncClient.setPropertiesWithResponse(properties).subscribe(r -> + System.out.printf("Setting File service properties completed with status code %d", r.statusCode())); + }); // END: com.azure.storage.file.fileServiceAsyncClient.setPropertiesWithResponseAsync#fileServiceProperties } @@ -253,12 +253,13 @@ public void setPropertiesWithResponseAsync() { public void clearPropertiesAsync() { FileServiceAsyncClient fileServiceAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.fileServiceAsyncClient.setPropertiesWithResponse#fileServiceProperties.clearCORS - FileServiceProperties properties = fileServiceAsyncClient.getPropertiesWithResponse().block().value(); - properties.cors(Collections.emptyList()); + fileServiceAsyncClient.getProperties().subscribe(properties -> { + properties.cors(Collections.emptyList()); - fileServiceAsyncClient.setPropertiesWithResponse(properties) - .subscribe(response -> System.out.printf("Setting File service properties completed with status code %d", - response.statusCode())); + fileServiceAsyncClient.setPropertiesWithResponse(properties).subscribe(response -> + System.out.printf("Setting File service properties completed with status code %d", + response.statusCode())); + }); // END: com.azure.storage.file.fileServiceAsyncClient.setPropertiesWithResponse#fileServiceProperties.clearCORS } } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceJavaDocCodeSamples.java index 0f7f44e084f3..27e7e5a13c90 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceJavaDocCodeSamples.java @@ -98,7 +98,7 @@ public void createShare() { FileServiceClient fileServiceClient = createClientWithSASToken(); // BEGIN: com.azure.storage.file.fileServiceClient.createShare#string fileServiceClient.createShare("myshare"); - System.out.printf("Creating the share completed."); + System.out.println("Creating the share completed."); // END: com.azure.storage.file.fileServiceClient.createShare#string } @@ -222,7 +222,7 @@ public void setProperties() { properties.hourMetrics().enabled(true); fileServiceClient.setProperties(properties); - System.out.printf("Setting File service properties completed."); + System.out.println("Setting File service properties completed."); // END: com.azure.storage.file.fileServiceClient.setProperties#fileServiceProperties } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceSample.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceSample.java index b0f8e8709b96..14652a8fcce1 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceSample.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/FileServiceSample.java @@ -41,7 +41,7 @@ public static void main(String[] args) { try { FileServiceProperties properties = fileServiceClient.getProperties(); - System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b\n", + System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n", properties.hourMetrics(), properties.minuteMetrics()); } catch (StorageErrorException e) { System.out.println("Failed to get the account properties. Reasons: " + e.getMessage()); @@ -50,7 +50,7 @@ public static void main(String[] args) { fileServiceClient.listShares().forEach( shareItem -> { try { - System.out.printf("This is the share name: %s in the file account.\n", shareItem.name()); + System.out.printf("This is the share name: %s in the file account.%n", shareItem.name()); fileServiceClient.deleteShare(shareItem.name()); System.out.println("The share has been deleted from the storage file account!"); } catch (StorageErrorException e) { diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareAsyncJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareAsyncJavaDocCodeSamples.java index f27798a0b7c2..c089ec60078a 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareAsyncJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareAsyncJavaDocCodeSamples.java @@ -198,7 +198,7 @@ public void createFileAsyncMaxOverload() { // BEGIN: com.azure.storage.file.shareAsyncClient.createFile#string-long-filehttpheaders-map FileHTTPHeaders httpHeaders = new FileHTTPHeaders().fileContentType("text/plain"); shareAsyncClient.createFile("myfile", 1024) - .doOnSuccess(response -> System.out.printf("Creating the file completed.")); + .doOnSuccess(response -> System.out.println("Creating the file completed.")); // END: com.azure.storage.file.shareAsyncClient.createFile#string-long-filehttpheaders-map } @@ -306,7 +306,7 @@ public void setQuotaAsync() { ShareAsyncClient shareAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.shareAsyncClient.setQuota shareAsyncClient.setQuota(1024).doOnSuccess(response -> - System.out.printf("Setting the share quota completed.") + System.out.println("Setting the share quota completed.") ); // END: com.azure.storage.file.shareAsyncClient.setQuota } @@ -332,7 +332,7 @@ public void setMetadataAsync() { ShareAsyncClient shareAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.shareAsyncClient.setMetadata#map shareAsyncClient.setMetadata(Collections.singletonMap("share", "updatedMetadata")).doOnSuccess(response -> - System.out.printf("Setting the share metadata completed.") + System.out.println("Setting the share metadata completed.") ); // END: com.azure.storage.file.shareAsyncClient.setMetadata#map } @@ -357,7 +357,7 @@ public void clearMetadataAsync() { ShareAsyncClient shareAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.file.shareAsyncClient.clearMetadata#map shareAsyncClient.setMetadata(null).doOnSuccess(response -> - System.out.printf("Setting the share metadata completed.") + System.out.println("Setting the share metadata completed.") ); // END: com.azure.storage.file.shareAsyncClient.clearMetadata#map } @@ -387,7 +387,7 @@ public void setAccessPolicyAsync() { SignedIdentifier permission = new SignedIdentifier().id("mypolicy").accessPolicy(accessPolicy); shareAsyncClient.setAccessPolicy(Collections.singletonList(permission)).doOnSuccess( - response -> System.out.printf("Setting access policies completed.")); + response -> System.out.println("Setting access policies completed.")); // END: com.azure.storage.file.shareAsyncClient.setAccessPolicy } @@ -441,7 +441,8 @@ public void getSnapshotIdAsync() { .shareName("myshare") .snapshot(currentTime.toString()) .buildAsyncClient(); - shareAysncClient.getSnapshotId(); + + System.out.printf("Snapshot ID: %s%n", shareAysncClient.getSnapshotId()); // END: com.azure.storage.file.shareAsyncClient.getSnapshotId } } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareJavaDocCodeSamples.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareJavaDocCodeSamples.java index 62c858a8428e..9c8fd1c821ce 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareJavaDocCodeSamples.java @@ -298,7 +298,7 @@ public void getPropertiesWithResponse() { public void setQuota() { ShareClient shareClient = createClientWithSASToken(); // BEGIN: com.azure.storage.file.shareClient.setQuota - System.out.printf("Setting the share quota completed." + shareClient.setQuota(1024)); + System.out.println("Setting the share quota completed." + shareClient.setQuota(1024)); // END: com.azure.storage.file.shareClient.setQuota } @@ -319,8 +319,8 @@ public void setQuotaWithResponse() { public void setMetadata() { ShareClient shareClient = createClientWithSASToken(); // BEGIN: com.azure.storage.file.shareClient.setMetadata#map - System.out.printf("Setting the share metadata.", shareClient.setMetadata( - Collections.singletonMap("share", "updatedMetadata"))); + shareClient.setMetadata(Collections.singletonMap("share", "updatedMetadata")); + System.out.println("Setting the share metadata."); // END: com.azure.storage.file.shareClient.setMetadata#map } @@ -344,7 +344,8 @@ public void setMetadataWithResponse() { public void clearMetadata() { ShareClient shareClient = createClientWithSASToken(); // BEGIN: com.azure.storage.file.shareClient.clearMetadata#map - System.out.printf("Clear metadata completed.", shareClient.setMetadata(null)); + shareClient.setMetadata(null); + System.out.println("Clear metadata completed."); // END: com.azure.storage.file.shareClient.clearMetadata#map } @@ -374,7 +375,7 @@ public void setAccessPolicy() { SignedIdentifier permission = new SignedIdentifier().id("mypolicy").accessPolicy(accessPolicy); shareClient.setAccessPolicy(Collections.singletonList(permission)); - System.out.printf("Setting access policies completed."); + System.out.println("Setting access policies completed."); // END: com.azure.storage.file.shareClient.setAccessPolicy } @@ -430,7 +431,8 @@ public void getSnapshotId() { .shareName("myshare") .snapshot(currentTime.toString()) .buildClient(); - shareClient.getSnapshotId(); + + System.out.printf("Snapshot ID: %s%n", shareClient.getSnapshotId()); // END: com.azure.storage.file.shareClient.getSnapshotId } } diff --git a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareSample.java b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareSample.java index 73f69901028a..6e16ec572046 100644 --- a/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareSample.java +++ b/sdk/storage/azure-storage-file/src/samples/java/com/azure/storage/file/ShareSample.java @@ -41,7 +41,7 @@ public static void main(String[] args) { try { shareClient.create(); } catch (StorageErrorException e) { - System.out.printf("Failed to create the share %s with share client. Reasons: %s\n", shareName, e.getMessage()); + System.out.printf("Failed to create the share %s with share client. Reasons: %s%n", shareName, e.getMessage()); } // Create 3 directories using share client for (int i = 0; i < 3; i++) { @@ -70,10 +70,13 @@ public static void main(String[] args) { // Get the properties of the share with first snapshot. - ShareClient shareClientWithSnapshot1 = null; + ShareClient shareClientWithSnapshot1 = new ShareClientBuilder() + .endpoint(ENDPOINT) + .shareName(shareName) + .snapshot(shareSnapshot1) + .buildClient(); + try { - shareClientWithSnapshot1 = new ShareClientBuilder().endpoint(ENDPOINT) - .shareName(shareName).snapshot(shareSnapshot1).buildClient(); ShareProperties shareProperties1 = shareClientWithSnapshot1.getProperties(); System.out.println("This is the first snapshot eTag: " + shareProperties1.etag()); } catch (StorageErrorException e) { @@ -81,10 +84,13 @@ public static void main(String[] args) { } // Get the properties of the share with second snapshot. - ShareClient shareClientWithSnapshot2 = null; + ShareClient shareClientWithSnapshot2 = new ShareClientBuilder() + .endpoint(ENDPOINT) + .shareName(shareName) + .snapshot(shareSnapshot2) + .buildClient(); + try { - shareClientWithSnapshot2 = new ShareClientBuilder().endpoint(ENDPOINT) - .shareName(shareName).snapshot(shareSnapshot2).buildClient(); ShareProperties shareProperties2 = shareClientWithSnapshot2.getProperties(); System.out.println("This is the second snapshot eTag: " + shareProperties2.etag()); } catch (StorageErrorException e) { @@ -93,11 +99,8 @@ public static void main(String[] args) { // Get the root directory and list all directories. try { - shareClient.getRootDirectoryClient().listFilesAndDirectories().forEach( - fileRef -> { - System.out.printf("Is the resource a file or directory?"); - } - ); + shareClient.getRootDirectoryClient().listFilesAndDirectories().forEach(resource -> + System.out.printf("Name: %s, Directory? %b%n", resource.name(), resource.isDirectory())); } catch (StorageErrorException e) { System.out.println("Failed to delete the share. Reasons: " + e.getMessage()); } diff --git a/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueJavaDocCodeSamples.java b/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueJavaDocCodeSamples.java index 1eea6a6485c0..1bf8f43b6352 100644 --- a/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueJavaDocCodeSamples.java @@ -219,7 +219,7 @@ public void updateMessage() { queueClient.dequeueMessages().forEach( dequeuedMessage -> { UpdatedMessage response = queueClient.updateMessage("newText", - dequeuedMessage.messageId(), dequeuedMessage.popReceipt(), null); + dequeuedMessage.messageId(), dequeuedMessage.popReceipt(), Duration.ofSeconds(5)); System.out.println("Complete updating the message."); } ); @@ -235,7 +235,8 @@ public void updateMessageWithResponse() { queueClient.dequeueMessages().forEach( dequeuedMessage -> { Response response = queueClient.updateMessageWithResponse("newText", - dequeuedMessage.messageId(), dequeuedMessage.popReceipt(), null, new Context(key1, value1)); + dequeuedMessage.messageId(), dequeuedMessage.popReceipt(), + Duration.ofSeconds(5), new Context(key1, value1)); System.out.println("Complete updating the message with status code " + response.statusCode()); } ); diff --git a/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueServiceAsyncJavaDocCodeSamples.java b/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueServiceAsyncJavaDocCodeSamples.java index a95c510e078f..aa6be4f8c1d7 100644 --- a/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueServiceAsyncJavaDocCodeSamples.java +++ b/sdk/storage/azure-storage-queue/src/samples/java/com/azure/storage/queue/QueueServiceAsyncJavaDocCodeSamples.java @@ -193,7 +193,7 @@ public void setPropertiesAsync() { // BEGIN: com.azure.storage.queue.queueServiceAsyncClient.setProperties#storageServiceProperties StorageServiceProperties properties = queueServiceAsyncClient.getProperties().block(); queueServiceAsyncClient.setProperties(properties) - .doOnSuccess(response -> System.out.printf("Setting Queue service properties completed.")); + .doOnSuccess(response -> System.out.println("Setting Queue service properties completed.")); // END: com.azure.storage.queue.queueServiceAsyncClient.setProperties#storageServiceProperties } @@ -217,11 +217,13 @@ public void setPropertiesWithResponse() { public void setPropertiesEnableMetrics() { QueueServiceAsyncClient queueServiceAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.queue.queueServiceAsyncClient.setPropertiesEnableMetrics#storageServiceProperties - StorageServiceProperties properties = queueServiceAsyncClient.getProperties().block(); - properties.minuteMetrics().enabled(true); - properties.hourMetrics().enabled(true); - queueServiceAsyncClient.setProperties(properties).subscribe( - response -> System.out.printf("Setting Queue service properties completed.")); + queueServiceAsyncClient.getProperties().subscribe(properties -> { + properties.minuteMetrics().enabled(true); + properties.hourMetrics().enabled(true); + + queueServiceAsyncClient.setProperties(properties).subscribe(response -> + System.out.println("Setting Queue service properties completed.")); + }); // END: com.azure.storage.queue.queueServiceAsyncClient.setPropertiesEnableMetrics#storageServiceProperties } @@ -231,12 +233,14 @@ public void setPropertiesEnableMetrics() { public void setPropertiesAsyncEnableMetrics() { QueueServiceAsyncClient queueServiceAsyncClient = createAsyncClientWithSASToken(); // BEGIN: com.azure.storage.queue.queueServiceAsyncClient.setPropertiesWithResponseEnableMetrics#storageServiceProperties - StorageServiceProperties properties = queueServiceAsyncClient.getProperties().block(); - properties.minuteMetrics().enabled(true); - properties.hourMetrics().enabled(true); - queueServiceAsyncClient.setPropertiesWithResponse(properties) - .subscribe(response -> System.out.printf("Setting Queue service properties completed with status code %d", - response.statusCode())); + queueServiceAsyncClient.getProperties().subscribe(properties -> { + properties.minuteMetrics().enabled(true); + properties.hourMetrics().enabled(true); + + queueServiceAsyncClient.setPropertiesWithResponse(properties).subscribe(response -> + System.out.printf("Setting Queue service properties completed with status code %d", + response.statusCode())); + }); // END: com.azure.storage.queue.queueServiceAsyncClient.setPropertiesWithResponseEnableMetrics#storageServiceProperties }