diff --git a/samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadBucket.java b/samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadBucket.java new file mode 100644 index 0000000000..5e09f5a911 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadBucket.java @@ -0,0 +1,60 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.transfermanager; + +// [START storage_transfer_manager_download_bucket] +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.transfermanager.DownloadResult; +import com.google.cloud.storage.transfermanager.ParallelDownloadConfig; +import com.google.cloud.storage.transfermanager.TransferManager; +import com.google.cloud.storage.transfermanager.TransferManagerConfig; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; + +class DownloadBucket { + + public static void downloadBucketContents(String projectId, + String bucketName, Path destinationDirectory) { + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); + List blobs = storage + .list(bucketName) + .streamAll() + .map(blob -> blob.asBlobInfo()) + .collect(Collectors.toList()); + TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); + ParallelDownloadConfig parallelDownloadConfig = ParallelDownloadConfig.newBuilder() + .setBucketName(bucketName) + .setDownloadDirectory(destinationDirectory) + .build(); + + List results = transferManager + .downloadBlobs(blobs, parallelDownloadConfig) + .getDownloadResults(); + + for (DownloadResult result : results) { + System.out.println("Download of " + result.getInput().getName() + + " completed with status " + + result.getStatus()); + } + + } + +} +// [END storage_transfer_manager_download_bucket] diff --git a/samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadMany.java b/samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadMany.java new file mode 100644 index 0000000000..4fd1720b38 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadMany.java @@ -0,0 +1,51 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.transfermanager; + +// [START storage_transfer_manager_download_many] +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.transfermanager.DownloadResult; +import com.google.cloud.storage.transfermanager.ParallelDownloadConfig; +import com.google.cloud.storage.transfermanager.TransferManager; +import com.google.cloud.storage.transfermanager.TransferManagerConfig; +import java.nio.file.Path; +import java.util.List; + +class DownloadMany { + + public static void downloadManyBlobs(String bucketName, + List blobs, Path destinationDirectory) { + + TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); + ParallelDownloadConfig parallelDownloadConfig = ParallelDownloadConfig.newBuilder() + .setBucketName(bucketName) + .setDownloadDirectory(destinationDirectory) + .build(); + + List results = transferManager + .downloadBlobs(blobs, parallelDownloadConfig) + .getDownloadResults(); + + for (DownloadResult result : results) { + System.out.println("Download of " + result.getInput().getName() + + " completed with status " + result.getStatus()); + } + + } + +} +// [END storage_transfer_manager_download_many] diff --git a/samples/snippets/src/main/java/com/example/storage/transfermanager/UploadDirectory.java b/samples/snippets/src/main/java/com/example/storage/transfermanager/UploadDirectory.java new file mode 100644 index 0000000000..096b2daedb --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/transfermanager/UploadDirectory.java @@ -0,0 +1,59 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.transfermanager; + +// [START storage_transfer_manager_upload_directory] +import com.google.cloud.storage.transfermanager.ParallelUploadConfig; +import com.google.cloud.storage.transfermanager.TransferManager; +import com.google.cloud.storage.transfermanager.TransferManagerConfig; +import com.google.cloud.storage.transfermanager.UploadResult; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +class UploadDirectory { + + public static void uploadDirectoryContents(String bucketName, Path sourceDirectory) + throws IOException { + TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); + ParallelUploadConfig parallelUploadConfig = ParallelUploadConfig + .newBuilder() + .setBucketName(bucketName) + .build(); + + // Create a list to store the file paths + List filePaths = new ArrayList<>(); + // Get all files in the directory + // try-with-resource to ensure pathStream is closed + try (Stream pathStream = Files.walk(sourceDirectory)) { + pathStream.filter(Files::isRegularFile) + .forEach(filePaths::add); + } + List results = transferManager + .uploadFiles(filePaths, parallelUploadConfig) + .getUploadResults(); + for (UploadResult result : results) { + System.out.println("Upload for " + result.getInput().getName() + + " completed with status " + result.getStatus()); + } + } + +} +// [END storage_transfer_manager_upload_directory] \ No newline at end of file diff --git a/samples/snippets/src/main/java/com/example/storage/transfermanager/UploadMany.java b/samples/snippets/src/main/java/com/example/storage/transfermanager/UploadMany.java new file mode 100644 index 0000000000..245df36492 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/transfermanager/UploadMany.java @@ -0,0 +1,46 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.transfermanager; + +// [START storage_transfer_manager_upload_many] +import com.google.cloud.storage.transfermanager.ParallelUploadConfig; +import com.google.cloud.storage.transfermanager.TransferManager; +import com.google.cloud.storage.transfermanager.TransferManagerConfig; +import com.google.cloud.storage.transfermanager.UploadResult; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + +class UploadMany { + + public static void uploadManyFiles(String bucketName, List files) + throws IOException { + TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); + ParallelUploadConfig parallelUploadConfig = ParallelUploadConfig + .newBuilder() + .setBucketName(bucketName) + .build(); + List results = transferManager + .uploadFiles(files, parallelUploadConfig) + .getUploadResults(); + for (UploadResult result : results) { + System.out.println("Upload for " + result.getInput().getName() + + " completed with status " + result.getStatus()); + } + } +} +// [END storage_transfer_manager_upload_many] diff --git a/samples/snippets/src/test/java/com/example/storage/transfermanager/ITTransferManagerSamples.java b/samples/snippets/src/test/java/com/example/storage/transfermanager/ITTransferManagerSamples.java new file mode 100644 index 0000000000..e43b3a16f7 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/transfermanager/ITTransferManagerSamples.java @@ -0,0 +1,119 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.example.storage.transfermanager; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.testing.RemoteStorageHelper; +import com.google.cloud.testing.junit4.StdOutCaptureRule; +import com.google.common.collect.ImmutableList; +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + + +public class ITTransferManagerSamples { + private static final String BUCKET = RemoteStorageHelper.generateBucketName(); + private static Storage storage; + private static List blobs; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + @Rule + public final StdOutCaptureRule stdOutCaptureRule = new StdOutCaptureRule(); + @Rule public final TemporaryFolder tmp = new TemporaryFolder(); + @Rule public final TemporaryFolder tmpDirectory = new TemporaryFolder(); + + @BeforeClass + public static void beforeClass() { + RemoteStorageHelper helper = RemoteStorageHelper.create(); + storage = helper.getOptions().getService(); + storage.create(BucketInfo.of(BUCKET)); + blobs = Arrays.asList(BlobInfo.newBuilder(BUCKET, "blob1").build(), + BlobInfo.newBuilder(BUCKET, "blob2").build(), + BlobInfo.newBuilder(BUCKET, "blob3").build()); + for (BlobInfo blob : blobs) { + storage.create(blob); + } + + } + + @Test + public void uploadFiles() throws Exception { + File tmpFile = File.createTempFile("file", ".txt"); + File tmpFile2 = File.createTempFile("file2", ".txt"); + File tmpFile3 = File.createTempFile("file3", ".txt"); + List files = + ImmutableList.of(tmpFile.toPath(), tmpFile2.toPath(), tmpFile3.toPath()); + UploadMany.uploadManyFiles(BUCKET, files); + String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); + assertThat(snippetOutput.contains("file")).isTrue(); + assertThat(snippetOutput.contains("file2")).isTrue(); + assertThat(snippetOutput.contains("file3")).isTrue(); + } + + @Test + public void uploadDirectory() throws IOException { + File tmpFile = tmpDirectory.newFile("fileDirUpload.txt"); + File tmpFile2 = tmpDirectory.newFile("fileDirUpload2.txt"); + File tmpFile3 = tmpDirectory.newFile("fileDirUpload3.txt"); + UploadDirectory.uploadDirectoryContents(BUCKET, tmpDirectory.getRoot().toPath()); + String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); + assertThat(snippetOutput.contains("fileDirUpload.txt")).isTrue(); + assertThat(snippetOutput.contains("fileDirUpload2.txt")).isTrue(); + assertThat(snippetOutput.contains("fileDirUpload3.txt")).isTrue(); + } + + @Test + public void downloadBucket() { + String downloadFullBucketName = RemoteStorageHelper.generateBucketName(); + storage.create(BucketInfo.of(downloadFullBucketName)); + List bucketBlobs = Arrays.asList( + BlobInfo.newBuilder(downloadFullBucketName, "bucketb1").build(), + BlobInfo.newBuilder(downloadFullBucketName, "bucketb2").build(), + BlobInfo.newBuilder(downloadFullBucketName, "bucketb3").build()); + for (BlobInfo blob : bucketBlobs) { + storage.create(blob); + } + DownloadBucket + .downloadBucketContents(PROJECT_ID, downloadFullBucketName, tmp.getRoot().toPath()); + String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); + assertThat(snippetOutput.contains("bucketb1")).isTrue(); + assertThat(snippetOutput.contains("bucketb2")).isTrue(); + assertThat(snippetOutput.contains("bucketb3")).isTrue(); + } + + @Test + public void downloadFiles() { + DownloadMany.downloadManyBlobs(BUCKET, blobs, tmp.getRoot().toPath()); + String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); + assertThat(snippetOutput.contains("blob1")).isTrue(); + assertThat(snippetOutput.contains("blob2")).isTrue(); + assertThat(snippetOutput.contains("blob3")).isTrue(); + } + +}