-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(storage): migrate transfer manager samples and tests #4257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
angelcaamal
wants to merge
4
commits into
GoogleCloudPlatform:main
Choose a base branch
from
angelcaamal:transferManager-storage-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5432e9a
feat(storage): introduce transfer manager samples and tests for migra…
angelcaamal fd823b8
test: fix typo in transfer-manager.test.js
angelcaamal 1c129a5
Merge branch 'main' into transferManager-storage-migration
iennae 583ed69
Merge branch 'main' into transferManager-storage-migration
angelcaamal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * Copyright 2022 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. | ||
| * | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
| const cwd = path.join(__dirname, '..'); | ||
|
|
||
| // sample-metadata: | ||
| // title: Download a File in Chunks With Transfer Manager | ||
| // description: Downloads a single file in in chunks in parallel utilizing transfer manager. | ||
| // usage: node downloadFileInChunksWithTransferManager.js <BUCKET_NAME> <FILE_NAME> <DESTINATION_FILE_NAME> <CHUNK_SIZE> | ||
|
|
||
| function main( | ||
| bucketName = 'my-bucket', | ||
| fileName = 'file1.txt', | ||
| destFileName = path.join(cwd, fileName), | ||
| chunkSize = 1024 | ||
| ) { | ||
| // [START storage_transfer_manager_download_chunks_concurrently] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of the GCS file to download | ||
| // const fileName = 'your-file-name'; | ||
|
|
||
| // The path to which the file should be downloaded | ||
| // const destFileName = '/local/path/to/file.txt'; | ||
|
|
||
| // The size of each chunk to be downloaded | ||
| // const chunkSize = 1024; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage, TransferManager} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| // Creates a transfer manager client | ||
| const transferManager = new TransferManager(storage.bucket(bucketName)); | ||
|
|
||
| async function downloadFileInChunksWithTransferManager() { | ||
| try { | ||
| // Downloads the files | ||
| await transferManager.downloadFileInChunks(fileName, { | ||
| destination: destFileName, | ||
| chunkSizeBytes: chunkSize, | ||
| }); | ||
|
|
||
| console.log( | ||
| `gs://${bucketName}/${fileName} downloaded to ${destFileName}.` | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error downloading file in chunks with Transfer Manager:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| downloadFileInChunksWithTransferManager(); | ||
| // [END storage_transfer_manager_download_chunks_concurrently] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /** | ||
| * Copyright 2022 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. | ||
| * | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Download Folder With Transfer Manager | ||
| // description: Downloads a folder in parallel utilizing transfer manager. | ||
| // usage: node downloadFolderWithTransferManager.js <BUCKET_NAME> <FOLDER_NAME> | ||
|
|
||
| function main(bucketName = 'my-bucket', folderName = 'my-folder') { | ||
| // [START storage_transfer_manager_download_folder] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of the GCS folder to download. The folder will be downloaded to the local path of the executing code. | ||
| // const folderName = 'your-folder-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage, TransferManager} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| // Creates a transfer manager client | ||
| const transferManager = new TransferManager(storage.bucket(bucketName)); | ||
|
|
||
| async function downloadFolderWithTransferManager() { | ||
| try { | ||
| // Downloads the folder | ||
| await transferManager.downloadManyFiles(folderName); | ||
|
|
||
| console.log( | ||
| `gs://${bucketName}/${folderName} downloaded to ${folderName}.` | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error downloading folder with Transfer Manager:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| downloadFolderWithTransferManager(); | ||
| // [END storage_transfer_manager_download_folder] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Copyright 2022 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. | ||
| * | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Download Many Files With Transfer Manager | ||
| // description: Downloads many files in parallel utilizing transfer manager. | ||
| // usage: node downloadManyFilesWithTransferManager.js <BUCKET_NAME> <FIRST_FILE_NAME> <SECOND_FILE_NAME> | ||
|
|
||
| function main( | ||
| bucketName = 'my-bucket', | ||
| firstFileName = 'file1.txt', | ||
| secondFileName = 'file2.txt' | ||
| ) { | ||
| // [START storage_transfer_manager_download_many] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of the first GCS file to download | ||
| // const firstFileName = 'your-first-file-name'; | ||
|
|
||
| // The ID of the second GCS file to download | ||
| // const secondFileName = 'your-second-file-name; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage, TransferManager} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| // Creates a transfer manager client | ||
| const transferManager = new TransferManager(storage.bucket(bucketName)); | ||
|
|
||
| async function downloadManyFilesWithTransferManager() { | ||
| try { | ||
| // Downloads the files | ||
| await transferManager.downloadManyFiles([firstFileName, secondFileName]); | ||
|
|
||
| for (const fileName of [firstFileName, secondFileName]) { | ||
| console.log( | ||
| `gs://${bucketName}/${fileName} downloaded to ${fileName}.` | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error downloading files with Transfer Manager:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| downloadManyFilesWithTransferManager(); | ||
| // [END storage_transfer_manager_download_many] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Copyright 2022 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
| const {Storage} = require('@google-cloud/storage'); | ||
| const {before, after, it, describe} = require('mocha'); | ||
| const uuid = require('uuid'); | ||
| const cp = require('child_process'); | ||
| const {assert} = require('chai'); | ||
|
|
||
| const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
| const storage = new Storage(); | ||
| const cwd = path.join(__dirname, '..'); | ||
| const bucketName = generateName(); | ||
| const bucket = storage.bucket(bucketName); | ||
| const firstFileName = 'test.txt'; | ||
| const secondFileName = 'test2.txt'; | ||
| const resourcesPath = path.join(cwd, 'resources'); | ||
| const firstFilePath = path.join(resourcesPath, firstFileName); | ||
| const secondFilePath = path.join(resourcesPath, secondFileName); | ||
| const downloadFilePath = path.join(cwd, 'downloaded.txt'); | ||
| const chunkSize = 1024; | ||
|
|
||
| describe('transfer manager', () => { | ||
| before(async () => { | ||
| await bucket.create(); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await bucket.deleteFiles({force: true}).catch(console.error); | ||
| await bucket.delete().catch(console.error); | ||
| }); | ||
|
|
||
| it('should upload multiple files', async () => { | ||
| const output = execSync( | ||
| `node uploadManyFilesWithTransferManager.js ${bucketName} ${firstFilePath} ${secondFilePath}` | ||
| ); | ||
| assert.match( | ||
| output, | ||
| new RegExp( | ||
| `${firstFilePath} uploaded to ${bucketName}.\n${secondFilePath} uploaded to ${bucketName}` | ||
| ) | ||
| ); | ||
| }); | ||
|
|
||
| it('should download multiple files', async () => { | ||
| const output = execSync( | ||
| `node downloadManyFilesWithTransferManager.js ${bucketName} ${firstFilePath} ${secondFilePath}` | ||
| ); | ||
| assert.match( | ||
| output, | ||
| new RegExp( | ||
| `gs://${bucketName}/${firstFilePath} downloaded to ${firstFilePath}.\ngs://${bucketName}/${secondFilePath} downloaded to ${secondFilePath}.` | ||
| ) | ||
| ); | ||
| }); | ||
|
|
||
| it('should download a file utilizing chunked download', async () => { | ||
| const output = execSync( | ||
| `node downloadFileInChunksWithTransferManager.js ${bucketName} ${firstFilePath} ${downloadFilePath} ${chunkSize}` | ||
| ); | ||
| assert.match( | ||
| output, | ||
| new RegExp( | ||
| `gs://${bucketName}/${firstFilePath} downloaded to ${downloadFilePath}.` | ||
| ) | ||
| ); | ||
| }); | ||
|
|
||
| it('should upload a file utilizing chunked upload', async () => { | ||
| const output = execSync( | ||
| `node uploadFileInChunksWithTransferManager.js ${bucketName} ${firstFilePath} ${chunkSize}` | ||
| ); | ||
| assert.match( | ||
| output, | ||
| new RegExp(`${firstFilePath} uploaded to ${bucketName}.`) | ||
| ); | ||
| }); | ||
|
|
||
| it('should upload a directory', async () => { | ||
| const output = execSync( | ||
| `node uploadDirectoryWithTransferManager.js ${bucketName} ${resourcesPath}` | ||
| ); | ||
| assert.match( | ||
| output, | ||
| new RegExp(`${resourcesPath} uploaded to ${bucketName}.`) | ||
| ); | ||
| }); | ||
|
|
||
| it('should download a directory', async () => { | ||
| const output = execSync( | ||
| `node downloadFolderWithTransferManager.js ${bucketName} ${resourcesPath}` | ||
| ); | ||
| assert.match( | ||
| output, | ||
| new RegExp( | ||
| `gs://${bucketName}/${resourcesPath} downloaded to ${resourcesPath}.` | ||
| ) | ||
| ); | ||
angelcaamal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
|
|
||
| function generateName() { | ||
| return `nodejs-storage-samples-${uuid.v4()}`; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.