diff --git a/circle.yml b/circle.yml index e67a8eb9d8a..68b59767062 100644 --- a/circle.yml +++ b/circle.yml @@ -109,7 +109,7 @@ deployment: - node scripts/build "resource" - node scripts/build "spanner" - node scripts/build "speech" - - node scripts/build "storage" + - node scripts/build "storage-transfer" - node scripts/build "trace" - node scripts/build "translate" # TODO: This build times out. Does video need more than 10 minutes? diff --git a/scripts/clean b/scripts/clean index be26fd50cfd..1a1dc7f82b2 100755 --- a/scripts/clean +++ b/scripts/clean @@ -22,8 +22,7 @@ const args = process.argv.slice(2); if (!args.length || args[0] === 'coverage') { rm('-rf', 'coverage'); } else if (args[0] === 'buckets') { - const NAME_REG_EXP = /^nodejs-docs-samples-test-[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/; - const NAME_REG_EXP_2 = /^docs-samples-gae-test-[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/; + const NAME_REG_EXP = /^docs-samples-gae-test-[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/; storage .getBuckets() @@ -31,7 +30,7 @@ if (!args.length || args[0] === 'coverage') { let promise = Promise.resolve(); buckets - .filter((bucket) => NAME_REG_EXP.test(bucket.name) || NAME_REG_EXP_2.test(bucket.name)) + .filter((bucket) => NAME_REG_EXP.test(bucket.name)) .forEach((bucket) => { promise = promise.then(() => { return bucket.deleteFiles() diff --git a/storage-transfer/README.md b/storage-transfer/README.md new file mode 100644 index 00000000000..75bfe78d2e7 --- /dev/null +++ b/storage-transfer/README.md @@ -0,0 +1,68 @@ +Google Cloud Platform logo + +# Google Cloud Storage Transfer API Node.js Samples + +[![Build](https://storage.googleapis.com/cloud-docs-samples-badges/GoogleCloudPlatform/nodejs-docs-samples/nodejs-docs-samples-storage-transfer.svg)]() + +## Table of Contents + +* [Setup](#setup) +* [Samples](#samples) + * [Storage Transfer API](#storage-transfer-api) +* [Running the tests](#running-the-tests) + +## Setup + +1. Read [Prerequisites][prereq] and [How to run a sample][run] first. +1. Install dependencies: + + With **npm**: + + npm install + + With **yarn**: + + yarn install + +[prereq]: ../README.md#prerequisites +[run]: ../README.md#how-to-run-a-sample + +## Samples + +### Storage Transfer API + +View the [documentation][transfer_0_docs] or the [source code][transfer_0_code]. + +__Usage:__ `node transfer.js --help` + +``` +Commands: + jobs [args] Run a job command. + operations [args] Run an operation command. + +Options: + --help Show help [boolean] + +Examples: + node transfer.js jobs --help Show job commands. + node transfer.js operations --help Show operations commands. + +For more information, see https://cloud.google.com/storage/transfer +``` + +[transfer_0_docs]: https://cloud.google.com/storage/transfer +[transfer_0_code]: transfer.js + +## Running the tests + +1. Set the **GCLOUD_PROJECT** and **GOOGLE_APPLICATION_CREDENTIALS** environment variables. + +1. Run the tests: + + With **npm**: + + npm test + + With **yarn**: + + yarn test diff --git a/storage-transfer/package.json b/storage-transfer/package.json new file mode 100644 index 00000000000..a033dc1c5d9 --- /dev/null +++ b/storage-transfer/package.json @@ -0,0 +1,48 @@ +{ + "name": "nodejs-docs-samples-storage-transfer", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google Inc.", + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": ">=4" + }, + "scripts": { + "lint": "samples lint", + "pretest": "npm run lint", + "unit-test": "samples test run --cmd ava -- -T 20s --verbose test/*.test.js", + "system-test": "samples test run --cmd ava -- -T 20s --verbose system-test/*.test.js", + "test": "npm run unit-test && npm run system-test" + }, + "dependencies": { + "googleapis": "21.3.0", + "moment": "2.18.1", + "safe-buffer": "5.1.1", + "yargs": "9.0.1" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "1.4.17", + "ava": "0.22.0", + "proxyquire": "1.8.0", + "sinon": "3.3.0", + "uuid": "3.1.0" + }, + "cloud-repo-tools": { + "requiresKeyFile": true, + "requiresProjectId": true, + "product": "storage", + "samples": [ + { + "id": "transfer", + "name": "Storage Transfer API", + "file": "transfer.js", + "docs_link": "https://cloud.google.com/storage/transfer", + "usage": "node transfer.js --help" + } + ] + } +} diff --git a/storage/system-test/transfer.test.js b/storage-transfer/system-test/transfer.test.js similarity index 100% rename from storage/system-test/transfer.test.js rename to storage-transfer/system-test/transfer.test.js diff --git a/storage/test/transfer.test.js b/storage-transfer/test/transfer.test.js similarity index 100% rename from storage/test/transfer.test.js rename to storage-transfer/test/transfer.test.js diff --git a/storage/transfer.js b/storage-transfer/transfer.js similarity index 100% rename from storage/transfer.js rename to storage-transfer/transfer.js diff --git a/storage/README.md b/storage/README.md index 0d4e140ffbf..d4bc05b8233 100644 --- a/storage/README.md +++ b/storage/README.md @@ -1,248 +1,5 @@ -Google Cloud Platform logo +Samples for the [Google Cloud Storage Node.js Client][client] have moved to +[github.com/googleapis/nodejs-storage: samples/][samples]. -# Google Cloud Storage Node.js Samples - -[![Build](https://storage.googleapis.com/cloud-docs-samples-badges/GoogleCloudPlatform/nodejs-docs-samples/nodejs-docs-samples-storage.svg)]() - -[Cloud Storage](https://cloud.google.com/storage/docs) allows world-wide storage and retrieval of any amount of data at any time. You can use Google Cloud Storage for a range of scenarios including serving website content, storing data for archival and disaster recovery, or distributing large data objects to users via direct download. - -## Table of Contents - -* [Setup](#setup) -* [Samples](#samples) - * [ACL (Access Control Lists)](#acl-access-control-lists) - * [Buckets](#buckets) - * [Encryption](#encryption) - * [Files](#files) - * [Storage Transfer API](#storage-transfer-api) - * [Requester Pays samples](#requester-pays-samples) -* [Running the tests](#running-the-tests) - -## Setup - -1. Read [Prerequisites][prereq] and [How to run a sample][run] first. -1. Install dependencies: - - With **npm**: - - npm install - - With **yarn**: - - yarn install - -[prereq]: ../README.md#prerequisites -[run]: ../README.md#how-to-run-a-sample - -## Samples - -### ACL (Access Control Lists) - -View the [documentation][acl_0_docs] or the [source code][acl_0_code]. - -__Usage:__ `node acl.js --help` - -``` -Commands: - print-bucket-acl Prints the ACL for a bucket. - print-bucket-acl-for-user Prints a user's ACL for a bucket. - add-bucket-owner Adds a user as an owner of a bucket. - remove-bucket-owner Removes a user from the ACL of a bucket. - add-bucket-default-owner Adds a user as an owner in the default ACL of a bucket. - remove-bucket-default-owner Removes a user from the default ACL of a bucket. - print-file-acl Prints the ACL for a file. - print-file-acl-for-user Prints a user's ACL for a file. - add-file-owner Adds a user as an owner of a file. - remove-file-owner Removes a user from the ACL of a file. - -Options: - --help Show help [boolean] - -Examples: - node acl.js print-bucket-acl my-bucket - node acl.js print-bucket-acl-for-user my-bucket bob@company.com - node acl.js add-bucket-owner my-bucket bob@company.com - node acl.js remove-bucket-owner my-bucket bob@company.com - node acl.js add-bucket-default-owner my-bucket bob@company.com - node acl.js remove-bucket-default-owner my-bucket bob@company.com - node acl.js print-file-acl my-bucket file.txt - node acl.js print-file-acl-for-user my-bucket file.txt bob@company.com - node acl.js add-file-owner my-bucket file.txt bob@company.com - node acl.js remove-file-owner my-bucket file.txt bob@company.com - -For more information, see https://cloud.google.com/storage/docs/access-control/create-manage-lists -``` - -[acl_0_docs]: https://cloud.google.com/storage/docs/access-control/create-manage-lists -[acl_0_code]: acl.js - -### Buckets - -View the [documentation][buckets_1_docs] or the [source code][buckets_1_code]. - -__Usage:__ `node buckets.js --help` - -``` -Commands: - create Creates a new bucket. - list Lists all buckets in the current project. - delete Deletes a bucket. - -Options: - --help Show help [boolean] - -Examples: - node buckets.js create my-bucket Creates a new bucket named "my-bucket". - node buckets.js list Lists all buckets in the current project. - node buckets.js delete my-bucket Deletes a bucket named "my-bucket". - -For more information, see https://cloud.google.com/storage/docs -``` - -[buckets_1_docs]: https://cloud.google.com/storage/docs -[buckets_1_code]: buckets.js - -### Encryption - -View the [documentation][encryption_2_docs] or the [source code][encryption_2_code]. - -__Usage:__ `node encryption.js --help` - -``` -Commands: - generate-encryption-key Generate a sample encryption key. - upload Encrypts and uploads a file. - download Decrypts and downloads a file. - rotate Rotates encryption keys for a file. - -Options: - --help Show help [boolean] - -Examples: - node encryption.js generate-encryption-key Generate a sample encryption key. - node encryption.js upload my-bucket ./resources/test.txt Encrypts and uploads "resources/test.txt" to - file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q= "gs://my-bucket/file_encrypted.txt". - node encryption.js download my-bucket file_encrypted.txt Decrypts and downloads - ./file.txt QxhqaZEqBGVTW55HhQw9Q= "gs://my-bucket/file_encrypted.txt" to "./file.txt". - node encryption.js rotate my-bucket file_encrypted.txt Rotates encryption keys for - QxhqaZEqBGVTW55HhQw9Q= SxafpsdfSDFS89sds9Q= "gs://my-bucket/file_encrypted.txt". - -For more information, see https://cloud.google.com/storage/docs -``` - -[encryption_2_docs]: https://cloud.google.com/storage/docs -[encryption_2_code]: encryption.js - -### Files - -View the [documentation][files_3_docs] or the [source code][files_3_code]. - -__Usage:__ `node files.js --help` - -``` -Commands: - list [prefix] [delimiter] Lists files in a bucket, optionally filtering by a - prefix. - upload Uploads a local file to a bucket. - download Downloads a file from a bucket. - delete Deletes a file from a bucket. - get-metadata Gets the metadata for a file. - make-public Makes a file public. - generate-signed-url Generates a signed URL for a file. - move Moves a file to a new location within the same bucket, - i.e. rename the file. - copy Copies a file in a bucket to another bucket. - - -Options: - --help Show help [boolean] - -Examples: - node files.js list my-bucket Lists files in "my-bucket". - node files.js list my-bucket public/ Lists files in "my-bucket" filtered by prefix "public/". - node files.js upload my-bucket ./file.txt Uploads "./file.txt" to "my-bucket". - node files.js download my-bucket file.txt ./file.txt Downloads "gs://my-bucket/file.txt" to "./file.txt". - node files.js delete my-bucket file.txt Deletes "gs://my-bucket/file.txt". - node files.js get-metadata my-bucket file.txt Gets the metadata for "gs://my-bucket/file.txt". - node files.js make-public my-bucket file.txt Makes "gs://my-bucket/file.txt" public. - node files.js move my-bucket file.txt file2.txt Renames "gs://my-bucket/file.txt" to - "gs://my-bucket/file2.txt". - node files.js copy my-bucket file.txt my-other-bucket Copies "gs://my-bucket/file.txt" to - file.txt "gs://my-other-bucket/file.txt". - -For more information, see https://cloud.google.com/storage/docs -``` - -[files_3_docs]: https://cloud.google.com/storage/docs -[files_3_code]: files.js - -### Storage Transfer API - -View the [documentation][transfer_4_docs] or the [source code][transfer_4_code]. - -__Usage:__ `node transfer.js --help` - -``` -Commands: - jobs [args] Run a job command. - operations [args] Run an operation command. - -Options: - --help Show help [boolean] - -Examples: - node transfer.js jobs --help Show job commands. - node transfer.js operations --help Show operations commands. - -For more information, see https://cloud.google.com/storage/transfer -``` - -[transfer_4_docs]: https://cloud.google.com/storage/transfer -[transfer_4_code]: transfer.js - -### Requester Pays samples - -View the [documentation][requesterPays_5_docs] or the [source code][requesterPays_5_code]. - -__Usage:__ `node requesterPays.js --help` - -``` -Commands: - enable Enables requester-pays requests on a bucket. - disable Disables requester-pays requests on a bucket. - get-status Determines whether requester-pays requests are enabled on a - bucket. - download Downloads a file from a bucket using requester-pays requests. - -Options: - --help Show help [boolean] - -Examples: - node requesterPays.js enable my-bucket Enables requester-pays requests on a bucket named - "my-bucket". - node requesterPays.js disable my-bucket Disables requester-pays requests on a bucket named - "my-bucket". - node requesterPays.js get-status my-bucket Determines whether requester-pays requests are enabled - for a bucket named "my-bucket". - node requesterPays.js download my-bucket file.txt ./file.txt Downloads "gs://my-bucket/file.txt" to "./file.txt" - using requester-pays requests. - -For more information, see https://cloud.google.com/storage/docs -``` - -[requesterPays_5_docs]: https://cloud.google.com/storage/docs -[requesterPays_5_code]: requesterPays.js - -## Running the tests - -1. Set the **GCLOUD_PROJECT** and **GOOGLE_APPLICATION_CREDENTIALS** environment variables. - -1. Run the tests: - - With **npm**: - - npm test - - With **yarn**: - - yarn test +[client]: https://github.com/googleapis/nodejs-storage +[samples]: https://github.com/googleapis/nodejs-storage/tree/master/samples diff --git a/storage/acl.js b/storage/acl.js deleted file mode 100644 index 0c8ee63ee1d..00000000000 --- a/storage/acl.js +++ /dev/null @@ -1,441 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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. - */ - -/** - * This application demonstrates how to perform basic operations on bucket and - * file Access Control Lists with the Google Cloud Storage API. - * - * For more information, see the README.md under /storage and the documentation - * at https://cloud.google.com/storage/docs. - */ - -'use strict'; - -function printBucketAcl (bucketName) { - // [START storage_print_bucket_acl] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Gets the ACL for the bucket - storage - .bucket(bucketName) - .acl - .get() - .then((results) => { - const acls = results[0]; - - acls.forEach((acl) => { - console.log(`${acl.role}: ${acl.entity}`); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_print_bucket_acl] -} - -function printBucketAclForUser (bucketName, userEmail) { - // [START storage_print_bucket_acl_for_user] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The email of the user to check, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - const options = { - // Specify the user - entity: `user-${userEmail}` - }; - - // Gets the user's ACL for the bucket - storage - .bucket(bucketName) - .acl - .get(options) - .then((results) => { - const aclObject = results[0]; - - console.log(`${aclObject.role}: ${aclObject.entity}`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_print_bucket_acl_for_user] -} - -function addBucketOwner (bucketName, userEmail) { - // [START storage_add_bucket_owner] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The email of the user to add, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - // Makes the user an owner of the bucket. You can use addAllUsers(), - // addDomain(), addProject(), addGroup(), and addAllAuthenticatedUsers() - // to grant access to different types of entities. You can also use "readers" - // and "writers" to grant different roles. - storage - .bucket(bucketName) - .acl - .owners - .addUser(userEmail) - .then(() => { - console.log(`Added user ${userEmail} as an owner on bucket ${bucketName}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_add_bucket_owner] -} - -function removeBucketOwner (bucketName, userEmail) { - // [START storage_remove_bucket_owner] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The email of the user to remove, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - // Removes the user from the access control list of the bucket. You can use - // deleteAllUsers(), deleteDomain(), deleteProject(), deleteGroup(), and - // deleteAllAuthenticatedUsers() to remove access for different types of entities. - storage - .bucket(bucketName) - .acl - .owners - .deleteUser(userEmail) - .then(() => { - console.log(`Removed user ${userEmail} from bucket ${bucketName}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_remove_bucket_owner] -} - -function addBucketDefaultOwner (bucketName, userEmail) { - // [START storage_add_bucket_default_owner] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The email of the user to add, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - // Makes the user an owner in the default ACL of the bucket. You can use - // addAllUsers(), addDomain(), addProject(), addGroup(), and - // addAllAuthenticatedUsers() to grant access to different types of entities. - // You can also use "readers" and "writers" to grant different roles. - storage - .bucket(bucketName) - .acl - .default - .owners - .addUser(userEmail) - .then(() => { - console.log(`Added user ${userEmail} as an owner on bucket ${bucketName}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_add_bucket_default_owner] -} - -function removeBucketDefaultOwner (bucketName, userEmail) { - // [START storage_remove_bucket_default_owner] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The email of the user to remove, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - // Removes the user from the access control list of the bucket. You can use - // deleteAllUsers(), deleteDomain(), deleteProject(), deleteGroup(), and - // deleteAllAuthenticatedUsers() to remove access for different types of entities. - storage - .bucket(bucketName) - .acl - .default - .owners - .deleteUser(userEmail) - .then(() => { - console.log(`Removed user ${userEmail} from bucket ${bucketName}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_remove_bucket_default_owner] -} - -function printFileAcl (bucketName, filename) { - // [START storage_print_file_acl] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to access, e.g. "file.txt" - // const filename = "file.txt"; - - // Instantiates a client - const storage = Storage(); - - // Gets the ACL for the file - storage - .bucket(bucketName) - .file(filename) - .acl - .get() - .then((results) => { - const acls = results[0]; - - acls.forEach((acl) => { - console.log(`${acl.role}: ${acl.entity}`); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_print_file_acl] -} - -function printFileAclForUser (bucketName, filename, userEmail) { - // [START storage_print_file_acl_for_user] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to access, e.g. "file.txt" - // const filename = "file.txt"; - - // The email of the user to check, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - const options = { - // Specify the user - entity: `user-${userEmail}` - }; - - // Gets the user's ACL for the file - storage - .bucket(bucketName) - .file(filename) - .acl - .get(options) - .then((results) => { - const aclObject = results[0]; - - console.log(`${aclObject.role}: ${aclObject.entity}`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_print_file_acl_for_user] -} - -function addFileOwner (bucketName, filename, userEmail) { - // [START storage_add_file_owner] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to access, e.g. "file.txt" - // const filename = "file.txt"; - - // The email of the user to add, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - // Makes the user an owner of the file. You can use addAllUsers(), - // addDomain(), addProject(), addGroup(), and addAllAuthenticatedUsers() - // to grant access to different types of entities. You can also use "readers" - // and "writers" to grant different roles. - storage - .bucket(bucketName) - .file(filename) - .acl - .owners - .addUser(userEmail) - .then(() => { - console.log(`Added user ${userEmail} as an owner on file ${filename}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_add_file_owner] -} - -// [START storage_remove_file_owner] -function removeFileOwner (bucketName, filename, userEmail) { - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to access, e.g. "file.txt" - // const filename = "file.txt"; - - // The email of the user to remove, e.g. "developer@company.com" - // const userEmail = "developer@company.com"; - - // Instantiates a client - const storage = Storage(); - - // Removes the user from the access control list of the file. You can use - // deleteAllUsers(), deleteDomain(), deleteProject(), deleteGroup(), and - // deleteAllAuthenticatedUsers() to remove access for different types of entities. - storage - .bucket(bucketName) - .file(filename) - .acl - .owners - .deleteUser(userEmail) - .then(() => { - console.log(`Removed user ${userEmail} from file ${filename}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); -} -// [END storage_remove_file_owner] - -const cli = require(`yargs`) - .demand(1) - .command( - `print-bucket-acl `, - `Prints the ACL for a bucket.`, - {}, - (opts) => printBucketAcl(opts.bucketName) - ) - .command( - `print-bucket-acl-for-user `, - `Prints a user's ACL for a bucket.`, - {}, - (opts) => printBucketAclForUser(opts.bucketName, opts.userEmail) - ) - .command( - `add-bucket-owner `, - `Adds a user as an owner of a bucket.`, - {}, - (opts) => addBucketOwner(opts.bucketName, opts.userEmail) - ) - .command( - `remove-bucket-owner `, - `Removes a user from the ACL of a bucket.`, - {}, - (opts) => removeBucketOwner(opts.bucketName, opts.userEmail) - ) - .command( - `add-bucket-default-owner `, - `Adds a user as an owner in the default ACL of a bucket.`, - {}, - (opts) => addBucketDefaultOwner(opts.bucketName, opts.userEmail) - ) - .command( - `remove-bucket-default-owner `, - `Removes a user from the default ACL of a bucket.`, - {}, - (opts) => removeBucketDefaultOwner(opts.bucketName, opts.userEmail) - ) - .command( - `print-file-acl `, - `Prints the ACL for a file.`, - {}, - (opts) => printFileAcl(opts.bucketName, opts.fileName) - ) - .command( - `print-file-acl-for-user `, - `Prints a user's ACL for a file.`, - {}, - (opts) => printFileAclForUser(opts.bucketName, opts.fileName, opts.userEmail) - ) - .command( - `add-file-owner `, - `Adds a user as an owner of a file.`, - {}, - (opts) => addFileOwner(opts.bucketName, opts.fileName, opts.userEmail) - ) - .command( - `remove-file-owner `, - `Removes a user from the ACL of a file.`, - {}, - (opts) => removeFileOwner(opts.bucketName, opts.fileName, opts.userEmail) - ) - .example(`node $0 print-bucket-acl my-bucket`) - .example(`node $0 print-bucket-acl-for-user my-bucket bob@company.com`) - .example(`node $0 add-bucket-owner my-bucket bob@company.com`) - .example(`node $0 remove-bucket-owner my-bucket bob@company.com`) - .example(`node $0 add-bucket-default-owner my-bucket bob@company.com`) - .example(`node $0 remove-bucket-default-owner my-bucket bob@company.com`) - .example(`node $0 print-file-acl my-bucket file.txt`) - .example(`node $0 print-file-acl-for-user my-bucket file.txt bob@company.com`) - .example(`node $0 add-file-owner my-bucket file.txt bob@company.com`) - .example(`node $0 remove-file-owner my-bucket file.txt bob@company.com`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/storage/docs/access-control/create-manage-lists`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/storage/buckets.js b/storage/buckets.js deleted file mode 100644 index e14261d78c7..00000000000 --- a/storage/buckets.js +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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. - */ - -/** - * This application demonstrates how to perform basic operations on buckets with - * the Google Cloud Storage API. - * - * For more information, see the README.md under /storage and the documentation - * at https://cloud.google.com/storage/docs. - */ - -'use strict'; - -function createBucket (bucketName) { - // [START storage_create_bucket] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to create, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Creates a new bucket - storage - .createBucket(bucketName) - .then(() => { - console.log(`Bucket ${bucketName} created.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_create_bucket] -} - -function listBuckets () { - // [START storage_list_buckets] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // Instantiates a client - const storage = Storage(); - - // Lists all buckets in the current project - storage - .getBuckets() - .then((results) => { - const buckets = results[0]; - - console.log('Buckets:'); - buckets.forEach((bucket) => { - console.log(bucket.name); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_list_buckets] -} - -function deleteBucket (bucketName) { - // [START storage_delete_bucket] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to delete, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Deletes the bucket - storage - .bucket(bucketName) - .delete() - .then(() => { - console.log(`Bucket ${bucketName} deleted.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_delete_bucket] -} - -const cli = require(`yargs`) - .demand(1) - .command( - `create `, - `Creates a new bucket.`, - {}, - (opts) => createBucket(opts.bucket) - ) - .command( - `list`, - `Lists all buckets in the current project.`, - {}, - listBuckets - ) - .command( - `delete `, - `Deletes a bucket.`, - {}, - (opts) => deleteBucket(opts.bucket) - ) - .example(`node $0 create my-bucket`, `Creates a new bucket named "my-bucket".`) - .example(`node $0 list`, `Lists all buckets in the current project.`) - .example(`node $0 delete my-bucket`, `Deletes a bucket named "my-bucket".`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/storage/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/storage/encryption.js b/storage/encryption.js deleted file mode 100644 index 30ee19e3490..00000000000 --- a/storage/encryption.js +++ /dev/null @@ -1,171 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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. - */ - -/** - * This application demonstrates how to perform basic operations on encrypted - * files with the Google Cloud Storage API. - * - * For more information, see the README.md under /storage and the documentation - * at https://cloud.google.com/storage/docs. - */ - -'use strict'; - -const Buffer = require('safe-buffer').Buffer; - -// [START storage_generate_encryption_key] -const crypto = require('crypto'); - -/** - * Generates a 256 bit (32 byte) AES encryption key and prints the base64 - * representation. - * - * This is included for demonstration purposes. You should generate your own - * key. Please remember that encryption keys should be handled with a - * comprehensive security policy. - * - * @returns {string} The encryption key. - */ -function generateEncryptionKey () { - const buffer = crypto.randomBytes(32); - const encodedKey = buffer.toString('base64'); - - console.log(`Base 64 encoded encryption key: ${encodedKey}`); - - return encodedKey; -} -// [END storage_generate_encryption_key] - -function uploadEncryptedFile (bucketName, srcFilename, destFilename, key) { - // [START storage_upload_encrypted_file] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the local file to upload, e.g. "./local/path/to/file.txt" - // const srcFilename = "./local/path/to/file.txt"; - - // The path to which the file should be uploaded, e.g. "file_encrypted.txt" - // const destFilename = "file.txt"; - - // Instantiates a client - const storage = Storage(); - - const options = { - // The path to which the file should be uploaded, e.g. "file_encrypted.txt" - destination: destFilename, - // Encrypt the file with a customer-supplied key, e.g. "my-secret-key" - encryptionKey: Buffer.from(key, 'base64') - }; - - // Encrypts and uploads a local file, e.g. "./local/path/to/file.txt". - // The file will only be retrievable using the key used to upload it. - storage - .bucket(bucketName) - .upload(srcFilename, options) - .then(() => { - console.log(`File ${srcFilename} uploaded to gs://${bucketName}/${destFilename}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_upload_encrypted_file] -} - -function downloadEncryptedFile (bucketName, srcFilename, destFilename, key) { - // [START storage_download_encrypted_file] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the remote file to download, e.g. "file_encrypted.txt" - // const srcFilename = "file_encrypted.txt"; - - // The path to which the file should be downloaded, e.g. "./file.txt" - // const destFilename = "./file.txt"; - - // Instantiates a client - const storage = Storage(); - - const options = { - // The path to which the file should be downloaded, e.g. "./file.txt" - destination: destFilename - }; - - // Descrypts and downloads the file. This can only be done with the key used - // to encrypt and upload the file. - storage - .bucket(bucketName) - .file(srcFilename) - .setEncryptionKey(Buffer.from(key, 'base64')) - .download(options) - .then(() => { - console.log(`File ${srcFilename} downloaded to ${destFilename}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_download_encrypted_file] -} - -// [START storage_rotate_encryption_key] -function rotateEncryptionKey () { - throw new Error('This is currently not available using the Cloud Client Library.'); -} -// [END storage_rotate_encryption_key] - -const cli = require(`yargs`) - .demand(1) - .command( - `generate-encryption-key`, - `Generate a sample encryption key.`, - {}, - generateEncryptionKey - ) - .command( - `upload `, - `Encrypts and uploads a file.`, - {}, - (opts) => uploadEncryptedFile(opts.bucketName, opts.srcFilename, opts.destFilename, opts.key) - ) - .command( - `download `, - `Decrypts and downloads a file.`, - {}, - (opts) => downloadEncryptedFile(opts.bucketName, opts.srcFilename, opts.destFilename, opts.key) - ) - .command( - `rotate `, - `Rotates encryption keys for a file.`, - {}, - rotateEncryptionKey - ) - .example(`node $0 generate-encryption-key`, `Generate a sample encryption key.`) - .example(`node $0 upload my-bucket ./resources/test.txt file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q=`, `Encrypts and uploads "resources/test.txt" to "gs://my-bucket/file_encrypted.txt".`) - .example(`node $0 download my-bucket file_encrypted.txt ./file.txt QxhqaZEqBGVTW55HhQw9Q=`, `Decrypts and downloads "gs://my-bucket/file_encrypted.txt" to "./file.txt".`) - .example(`node $0 rotate my-bucket file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q= SxafpsdfSDFS89sds9Q=`, `Rotates encryption keys for "gs://my-bucket/file_encrypted.txt".`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/storage/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/storage/files.js b/storage/files.js deleted file mode 100644 index 5e4ad074d47..00000000000 --- a/storage/files.js +++ /dev/null @@ -1,464 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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. - */ - -/** - * This application demonstrates how to perform basic operations on files with - * the Google Cloud Storage API. - * - * For more information, see the README.md under /storage and the documentation - * at https://cloud.google.com/storage/docs. - */ - -'use strict'; - -function listFiles (bucketName) { - // [START storage_list_files] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Lists files in the bucket - storage - .bucket(bucketName) - .getFiles() - .then((results) => { - const files = results[0]; - - console.log('Files:'); - files.forEach((file) => { - console.log(file.name); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_list_files] -} - -function listFilesByPrefix (bucketName, prefix, delimiter) { - // [START storage_list_files_with_prefix] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The prefix by which to filter files, e.g. "public/" - // const prefix = "public/"; - - // The delimiter to use, e.g. "/" - // const delimiter = "/"; - - // Instantiates a client - const storage = Storage(); - - /** - * This can be used to list all blobs in a "folder", e.g. "public/". - * - * The delimiter argument can be used to restrict the results to only the - * "files" in the given "folder". Without the delimiter, the entire tree under - * the prefix is returned. For example, given these blobs: - * - * /a/1.txt - * /a/b/2.txt - * - * If you just specify prefix = '/a', you'll get back: - * - * /a/1.txt - * /a/b/2.txt - * - * However, if you specify prefix='/a' and delimiter='/', you'll get back: - * - * /a/1.txt - */ - const options = { - prefix: prefix - }; - - if (delimiter) { - options.delimiter = delimiter; - } - - // Lists files in the bucket, filtered by a prefix - storage - .bucket(bucketName) - .getFiles(options) - .then((results) => { - const files = results[0]; - - console.log('Files:'); - files.forEach((file) => { - console.log(file.name); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_list_files_with_prefix] -} - -function uploadFile (bucketName, filename) { - // [START storage_upload_file] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the local file to upload, e.g. "./local/path/to/file.txt" - // const filename = "./local/path/to/file.txt"; - - // Instantiates a client - const storage = Storage(); - - // Uploads a local file to the bucket - storage - .bucket(bucketName) - .upload(filename) - .then(() => { - console.log(`${filename} uploaded to ${bucketName}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_upload_file] -} - -function downloadFile (bucketName, srcFilename, destFilename) { - // [START storage_download_file] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the remote file to download, e.g. "file.txt" - // const srcFilename = "file.txt"; - - // The path to which the file should be downloaded, e.g. "./local/path/to/file.txt" - // const destFilename = "./local/path/to/file.txt"; - - // Instantiates a client - const storage = Storage(); - - const options = { - // The path to which the file should be downloaded, e.g. "./file.txt" - destination: destFilename - }; - - // Downloads the file - storage - .bucket(bucketName) - .file(srcFilename) - .download(options) - .then(() => { - console.log(`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_download_file] -} - -function deleteFile (bucketName, filename) { - // [START storage_delete_file] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to delete, e.g. "file.txt" - // const filename = "file.txt"; - - // Instantiates a client - const storage = Storage(); - - // Deletes the file from the bucket - storage - .bucket(bucketName) - .file(filename) - .delete() - .then(() => { - console.log(`gs://${bucketName}/${filename} deleted.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_delete_file] -} - -function getMetadata (bucketName, filename) { - // [START storage_get_metadata] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to access, e.g. "file.txt" - // const filename = "file.txt"; - - // Instantiates a client - const storage = Storage(); - - // Gets the metadata for the file - storage - .bucket(bucketName) - .file(filename) - .getMetadata() - .then((results) => { - const metadata = results[0]; - - console.log(`File: ${metadata.name}`); - console.log(`Bucket: ${metadata.bucket}`); - console.log(`Storage class: ${metadata.storageClass}`); - console.log(`ID: ${metadata.id}`); - console.log(`Size: ${metadata.size}`); - console.log(`Updated: ${metadata.updated}`); - console.log(`Generation: ${metadata.generation}`); - console.log(`Metageneration: ${metadata.metageneration}`); - console.log(`Etag: ${metadata.etag}`); - console.log(`Owner: ${metadata.owner}`); - console.log(`Component count: ${metadata.component_count}`); - console.log(`Crc32c: ${metadata.crc32c}`); - console.log(`md5Hash: ${metadata.md5Hash}`); - console.log(`Cache-control: ${metadata.cacheControl}`); - console.log(`Content-type: ${metadata.contentType}`); - console.log(`Content-disposition: ${metadata.contentDisposition}`); - console.log(`Content-encoding: ${metadata.contentEncoding}`); - console.log(`Content-language: ${metadata.contentLanguage}`); - console.log(`Metadata: ${metadata.metadata}`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_get_metadata] -} - -function makePublic (bucketName, filename) { - // [START storage_make_public] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to make public, e.g. "file.txt" - // const filename = "file.txt"; - - // Instantiates a client - const storage = Storage(); - - // Makes the file public - storage - .bucket(bucketName) - .file(filename) - .makePublic() - .then(() => { - console.log(`gs://${bucketName}/${filename} is now public.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_make_public] -} - -function generateSignedUrl (bucketName, filename) { - // [START storage_generate_signed_url] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to access, e.g. "file.txt" - // const filename = "file.txt"; - - // Instantiates a client - const storage = Storage(); - - // These options will allow temporary read access to the file - const options = { - action: 'read', - expires: '03-17-2025' - }; - - // Get a signed URL for the file - storage - .bucket(bucketName) - .file(filename) - .getSignedUrl(options) - .then((results) => { - const url = results[0]; - - console.log(`The signed url for ${filename} is ${url}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_generate_signed_url] -} - -function moveFile (bucketName, srcFilename, destFilename) { - // [START storage_move_file] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the file to move, e.g. "file.txt" - // const srcFilename = "file.txt"; - - // The destination path for the file, e.g. "moved.txt" - // const destFilename = "moved.txt"; - - // Instantiates a client - const storage = Storage(); - - // Moves the file within the bucket - storage - .bucket(bucketName) - .file(srcFilename) - .move(destFilename) - .then(() => { - console.log(`gs://${bucketName}/${srcFilename} moved to gs://${bucketName}/${destFilename}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_move_file] -} - -function copyFile (srcBucketName, srcFilename, destBucketName, destFilename) { - // [START storage_copy_file] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the source bucket, e.g. "my-bucket" - // const srcBucketName = "my-bucket"; - - // The name of the source file, e.g. "file.txt" - // const srcFilename = "file.txt"; - - // The destination bucket, e.g. "my-other-bucket" - // const destBucketName = "my-other-bucket"; - - // The destination filename, e.g. "file.txt" - // const destFilename = "file.txt"; - - // Instantiates a client - const storage = Storage(); - - // Copies the file to the other bucket - storage - .bucket(srcBucketName) - .file(srcFilename) - .copy(storage.bucket(destBucketName).file(destFilename)) - .then(() => { - console.log(`gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END storage_copy_file] -} - -const cli = require(`yargs`) - .demand(1) - .command( - `list [prefix] [delimiter]`, - `Lists files in a bucket, optionally filtering by a prefix.`, - {}, - (opts) => { - if (opts.prefix) { - listFilesByPrefix(opts.bucketName, opts.prefix, opts.delimiter); - } else { - listFiles(opts.bucketName); - } - } - ) - .command( - `upload `, - `Uploads a local file to a bucket.`, - {}, - (opts) => uploadFile(opts.bucketName, opts.srcFileName) - ) - .command( - `download `, - `Downloads a file from a bucket.`, - {}, - (opts) => downloadFile(opts.bucketName, opts.srcFileName, opts.destFileName) - ) - .command( - `delete `, - `Deletes a file from a bucket.`, - {}, - (opts) => deleteFile(opts.bucketName, opts.fileName) - ) - .command( - `get-metadata `, - `Gets the metadata for a file.`, - {}, - (opts) => getMetadata(opts.bucketName, opts.fileName) - ) - .command( - `make-public `, - `Makes a file public.`, - {}, - (opts) => makePublic(opts.bucketName, opts.fileName) - ) - .command( - `generate-signed-url `, - `Generates a signed URL for a file.`, - {}, - (opts) => generateSignedUrl(opts.bucketName, opts.fileName) - ) - .command( - `move `, - `Moves a file to a new location within the same bucket, i.e. rename the file.`, - {}, - (opts) => moveFile(opts.bucketName, opts.srcFileName, opts.destFileName) - ) - .command( - `copy `, - `Copies a file in a bucket to another bucket.`, - {}, - (opts) => copyFile(opts.srcBucketName, opts.srcFileName, opts.destBucketName, opts.destFileName) - ) - .example(`node $0 list my-bucket`, `Lists files in "my-bucket".`) - .example(`node $0 list my-bucket public/`, `Lists files in "my-bucket" filtered by prefix "public/".`) - .example(`node $0 upload my-bucket ./file.txt`, `Uploads "./file.txt" to "my-bucket".`) - .example(`node $0 download my-bucket file.txt ./file.txt`, `Downloads "gs://my-bucket/file.txt" to "./file.txt".`) - .example(`node $0 delete my-bucket file.txt`, `Deletes "gs://my-bucket/file.txt".`) - .example(`node $0 get-metadata my-bucket file.txt`, `Gets the metadata for "gs://my-bucket/file.txt".`) - .example(`node $0 make-public my-bucket file.txt`, `Makes "gs://my-bucket/file.txt" public.`) - .example(`node $0 move my-bucket file.txt file2.txt`, `Renames "gs://my-bucket/file.txt" to "gs://my-bucket/file2.txt".`) - .example(`node $0 copy my-bucket file.txt my-other-bucket file.txt`, `Copies "gs://my-bucket/file.txt" to "gs://my-other-bucket/file.txt".`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/storage/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/storage/iam.js b/storage/iam.js deleted file mode 100644 index aa90cdd4f38..00000000000 --- a/storage/iam.js +++ /dev/null @@ -1,190 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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'; - -function viewBucketIamMembers (bucketName) { - // [START view_bucket_iam_members] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Gets and displays the bucket's IAM policy - storage - .bucket(bucketName) - .iam - .getPolicy() - .then((results) => { - const policy = results[0].bindings; - - // Displays the roles in the bucket's IAM policy - console.log(`Roles for bucket ${bucketName}:`); - policy.forEach((role) => { - console.log(` Role: ${role.role}`); - console.log(` Members:`); - - const members = role.members; - members.forEach((member) => { - console.log(` ${member}`); - }); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END view_bucket_iam_members] -} - -function addBucketIamMember (bucketName, roleName, members) { - // [START add_bucket_iam_member] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The bucket-level IAM role to grant, e.g. "roles/storage.objectViewer" - // const roleName = "roles/storage.objectViewer"; - - // The list of IAM members to grant the role to, e.g. ['user:jdoe@example.com', 'group:admins@example.com'] - // const members = ['user:jdoe@example.com', 'group:admins@example.com']; - - // Instantiates a client - const storage = Storage(); - - // Get a reference to a Google Cloud Storage bucket - const bucket = storage.bucket(bucketName); - - // Gets and updates the bucket's IAM policy - bucket.iam.getPolicy() - .then((results) => { - const policy = results[0]; - - // Adds the new roles to the bucket's IAM policy - policy.bindings.push({ - role: roleName, - members: members - }); - - // Updates the bucket's IAM policy - return bucket.iam.setPolicy(policy); - }) - .then(() => { - console.log(`Added the following member(s) with role ${roleName} to ${bucketName}:`); - members.forEach((member) => { - console.log(` ${member}`); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END add_bucket_iam_member] -} - -function removeBucketIamMember (bucketName, roleName, members) { - // [START remove_bucket_iam_member] - // Imports the Google Cloud client library - const Storage = require('@google-cloud/storage'); - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The bucket-level IAM role to grant, e.g. "roles/storage.objectViewer" - // const roleName = "roles/storage.objectViewer"; - - // The list of IAM members to grant the role to, e.g. ['user:jdoe@example.com', 'group:admins@example.com'] - // const members = ['user:jdoe@example.com', 'group:admins@example.com']; - - // Instantiates a client - const storage = Storage(); - - // Get a reference to a Google Cloud Storage bucket - const bucket = storage.bucket(bucketName); - - // Gets and updates the bucket's IAM policy - bucket.iam.getPolicy() - .then((data) => { - const policy = data[0]; - - // Finds and updates the appropriate role-member group - const index = policy.bindings.findIndex((role) => role.role === roleName); - let role = policy.bindings[index]; - if (role) { - role.members = role.members.filter((member) => members.indexOf(member) === -1); - - // Updates the policy object with the new (or empty) role-member group - if (role.members.length === 0) { - policy.bindings.splice(index, 1); - } else { - policy.bindings.index = role; - } - - // Updates the bucket's IAM policy - return bucket.iam.setPolicy(policy); - } else { - // No matching role-member group(s) were found - throw new Error('No matching role-member group(s) found.'); - } - }) - .then(() => { - console.log(`Removed the following member(s) with role ${roleName} from ${bucketName}:`); - members.forEach((member) => { - console.log(` ${member}`); - }); - }) - .catch((err) => { - console.error('ERROR:', err); - }); - // [END remove_bucket_iam_member] -} - -const cli = require(`yargs`) - .demand(1) - .array('members') - .command( - `view-members `, - `Lists IAM member-role groups for a given Google Cloud Storage bucket.`, - {}, - (opts) => viewBucketIamMembers(opts.bucketName) - ) - .command( - `add-members [members..]`, - `Adds one or more IAM member-role groups to a Google Cloud Storage bucket.`, - {}, - (opts) => addBucketIamMember(opts.bucketName, opts.roleName, opts.members) - ) - .command( - `remove-members [members..]`, - `Removes one or more IAM member-role groups from a Google Cloud Storage bucket.`, - {}, - (opts) => removeBucketIamMember(opts.bucketName, opts.roleName, opts.members) - ) - .example(`node $0 view-members "my-bucket"`) - .example(`node $0 add-members "my-bucket" "storage.objectViewer" "user:jdoe@example.com" "group:admins@example.com"`) - .example(`node $0 remove-members "my-bucket" "storage.objectViewer" "user:jdoe@example.com" "group:admins@example.com"`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/iam/docs/overview and https://cloud.google.com/storage/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/storage/package.json b/storage/package.json deleted file mode 100644 index 3c5ccdfb49c..00000000000 --- a/storage/package.json +++ /dev/null @@ -1,84 +0,0 @@ -{ - "name": "nodejs-docs-samples-storage", - "version": "0.0.1", - "private": true, - "license": "Apache-2.0", - "author": "Google Inc.", - "repository": { - "type": "git", - "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" - }, - "engines": { - "node": ">=4.3.2" - }, - "scripts": { - "lint": "samples lint", - "pretest": "npm run lint", - "unit-test": "ava -T 20s --verbose test/*.test.js", - "system-test": "ava -T 20s --verbose system-test/*.test.js", - "test": "npm run unit-test && npm run system-test" - }, - "dependencies": { - "@google-cloud/storage": "1.2.1", - "googleapis": "20.1.0", - "moment": "2.18.1", - "safe-buffer": "5.1.1", - "yargs": "8.0.2" - }, - "devDependencies": { - "@google-cloud/nodejs-repo-tools": "1.4.17", - "ava": "0.21.0", - "proxyquire": "1.8.0", - "sinon": "3.2.0", - "uuid": "3.1.0" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true, - "product": "storage", - "samples": [ - { - "id": "acl", - "name": "ACL (Access Control Lists)", - "file": "acl.js", - "docs_link": "https://cloud.google.com/storage/docs/access-control/create-manage-lists", - "usage": "node acl.js --help" - }, - { - "id": "buckets", - "name": "Buckets", - "file": "buckets.js", - "docs_link": "https://cloud.google.com/storage/docs", - "usage": "node buckets.js --help" - }, - { - "id": "encryption", - "name": "Encryption", - "file": "encryption.js", - "docs_link": "https://cloud.google.com/storage/docs", - "usage": "node encryption.js --help" - }, - { - "id": "files", - "name": "Files", - "file": "files.js", - "docs_link": "https://cloud.google.com/storage/docs", - "usage": "node files.js --help" - }, - { - "id": "transfer", - "name": "Storage Transfer API", - "file": "transfer.js", - "docs_link": "https://cloud.google.com/storage/transfer", - "usage": "node transfer.js --help" - }, - { - "id": "requesterPays", - "name": "Requester Pays samples", - "file": "requesterPays.js", - "docs_link": "https://cloud.google.com/storage/docs", - "usage": "node requesterPays.js --help" - } - ] - } -} diff --git a/storage/quickstart.js b/storage/quickstart.js deleted file mode 100644 index bc6e3b8b82b..00000000000 --- a/storage/quickstart.js +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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'; - -// [START storage_quickstart] -// Imports the Google Cloud client library -const Storage = require('@google-cloud/storage'); - -// Your Google Cloud Platform project ID -const projectId = 'YOUR_PROJECT_ID'; - -// Instantiates a client -const storage = Storage({ - projectId: projectId -}); - -// The name for the new bucket -const bucketName = 'my-new-bucket'; - -// Creates the new bucket -storage.createBucket(bucketName) - .then(() => { - console.log(`Bucket ${bucketName} created.`); - }) - .catch((err) => { - console.error('ERROR:', err); - }); -// [END storage_quickstart] diff --git a/storage/requesterPays.js b/storage/requesterPays.js deleted file mode 100644 index 28a2916dbb6..00000000000 --- a/storage/requesterPays.js +++ /dev/null @@ -1,191 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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. - */ - -/** - * This application demonstrates how to perform basic operations on buckets with - * the Google Cloud Storage API. - * - * For more information, see the README.md under /storage and the documentation - * at https://cloud.google.com/storage/docs. - */ - -'use strict'; - -function enableRequesterPays (bucketName) { - // [START enable_requester_pays] - // Imports the Google Cloud client library - const Storage = require(`@google-cloud/storage`); - - // The name of the bucket to enable requester-paying for, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Enables requester-pays requests - storage - .bucket(bucketName) - .enableRequesterPays() - .then(() => { - console.log(`Requester-pays requests have been enabled for bucket ${bucketName}.`); - }) - .catch((err) => { - console.error(`ERROR:`, err); - }); - // [END enable_requester_pays] -} - -function disableRequesterPays (bucketName) { - // [START disable_requester_pays] - // Imports the Google Cloud client library - const Storage = require(`@google-cloud/storage`); - - // The name of the bucket to disable requester-paying for, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Disables requester-pays requests - storage - .bucket(bucketName) - .disableRequesterPays() - .then(() => { - console.log(`Requester-pays requests have been disabled for bucket ${bucketName}.`); - }) - .catch((err) => { - console.error(`ERROR:`, err); - }); - // [END disable_requester_pays] -} - -function getRequesterPaysStatus (bucketName) { - // [START get_requester_pays_status] - // Imports the Google Cloud client library - const Storage = require(`@google-cloud/storage`); - - // The name of the bucket to get the requester-payable status for, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // Instantiates a client - const storage = Storage(); - - // Gets the requester-pays status of a bucket - storage - .bucket(bucketName) - .getMetadata() - .then((data) => { - let status; - const metadata = data[0]; - if (metadata && metadata.billing && metadata.billing.requesterPays) { - status = `enabled`; - } else { - status = `disabled`; - } - console.log(`Requester-pays requests are ${status} for bucket ${bucketName}.`); - }) - .catch((err) => { - console.error(`ERROR:`, err); - }); - // [END get_requester_pays_status] -} - -function downloadFileUsingRequesterPays (projectId, bucketName, srcFilename, destFilename) { - // [START storage_download_file_requester_pays] - // Imports the Google Cloud client library - const Storage = require(`@google-cloud/storage`); - - // The project ID to bill from - // const projectId = process.env.GCLOUD_PROJECT; - - // The name of the bucket to access, e.g. "my-bucket" - // const bucketName = "my-bucket"; - - // The name of the remote file to download, e.g. "file.txt" - // const srcFilename = "file.txt"; - - // The path to which the file should be downloaded, e.g. "./local/path/to/file.txt" - // const destFilename = "./local/path/to/file.txt"; - - // Instantiates a client - const storage = Storage(); - - const options = { - // The path to which the file should be downloaded, e.g. "./file.txt" - destination: destFilename, - - // The project to bill from, if requester-pays requests are enabled - userProject: projectId - }; - - // Downloads the file - storage - .bucket(bucketName) - .file(srcFilename) - .download(options) - .then(() => { - console.log(`gs://${bucketName}/${srcFilename} downloaded to ${destFilename} using requester-pays requests.`); - }) - .catch((err) => { - console.error(`ERROR:`, err); - }); - // [END storage_download_file_requester_pays] -} - -const cli = require(`yargs`) - .demand(1) - .command( - `enable `, - `Enables requester-pays requests on a bucket.`, - {}, - (opts) => enableRequesterPays(opts.bucket) - ) - .command( - `disable `, - `Disables requester-pays requests on a bucket.`, - {}, - (opts) => disableRequesterPays(opts.bucket) - ) - .command( - `get-status `, - `Determines whether requester-pays requests are enabled on a bucket.`, - {}, - (opts) => getRequesterPaysStatus(opts.bucket) - ) - .command( - `download `, - `Downloads a file from a bucket using requester-pays requests.`, - { - projectId: { - type: `string`, - alias: `p`, - default: process.env.GCLOUD_PROJECT - } - }, - (opts) => downloadFileUsingRequesterPays(opts.projectId, opts.bucketName, opts.srcFileName, opts.destFileName) - ) - .example(`node $0 enable my-bucket`, `Enables requester-pays requests on a bucket named "my-bucket".`) - .example(`node $0 disable my-bucket`, `Disables requester-pays requests on a bucket named "my-bucket".`) - .example(`node $0 get-status my-bucket`, `Determines whether requester-pays requests are enabled for a bucket named "my-bucket".`) - .example(`node $0 download my-bucket file.txt ./file.txt`, `Downloads "gs://my-bucket/file.txt" to "./file.txt" using requester-pays requests.`) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/storage/docs`) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/storage/resources/test.txt b/storage/resources/test.txt deleted file mode 100644 index c57eff55ebc..00000000000 --- a/storage/resources/test.txt +++ /dev/null @@ -1 +0,0 @@ -Hello World! \ No newline at end of file diff --git a/storage/system-test/acl.test.js b/storage/system-test/acl.test.js deleted file mode 100644 index 5b19d33b140..00000000000 --- a/storage/system-test/acl.test.js +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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 test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const bucket = storage.bucket(bucketName); -const userEmail = `jdobry@google.com`; -const fileName = `test.txt`; -const filePath = path.join(__dirname, `../resources`, fileName); -const cmd = `node acl.js`; - -test.before(tools.checkCredentials); -test.before(async (t) => { - await bucket.create(); - await bucket.upload(filePath); -}); - -test.after.always(async (t) => { - // Try deleting all files twice - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.delete(); - } catch (err) {} // ignore error -}); - -test(`should print acl for a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} print-bucket-acl ${bucketName}`, cwd); - t.true(output.includes(`OWNER: project-editors-`)); - t.true(output.includes(`OWNER: project-owners-`)); - t.true(output.includes(`READER: project-viewers-`)); -}); - -test(`should print acl for a file`, async (t) => { - const output = await tools.runAsync(`${cmd} print-file-acl ${bucketName} ${fileName}`, cwd); - t.true(output.includes(`OWNER: project-editors-`)); - t.true(output.includes(`OWNER: project-owners-`)); - t.true(output.includes(`READER: project-viewers-`)); -}); - -test.serial(`should print a user's acl for a bucket`, async (t) => { - await bucket.acl.readers.addUser(userEmail); - const output = await tools.runAsync(`${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}`, cwd); - t.is(output, `READER: user-${userEmail}`); - await bucket.acl.readers.deleteUser(userEmail); -}); - -test.serial(`should add a user as an owner on a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} add-bucket-owner ${bucketName} ${userEmail}`, cwd); - t.is(output, `Added user ${userEmail} as an owner on bucket ${bucketName}.`); -}); - -test.serial(`should remove a user from a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} remove-bucket-owner ${bucketName} ${userEmail}`, cwd); - t.is(output, `Removed user ${userEmail} from bucket ${bucketName}.`); -}); - -test.serial(`should add a user as a default owner on a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} add-bucket-default-owner ${bucketName} ${userEmail}`, cwd); - t.is(output, `Added user ${userEmail} as an owner on bucket ${bucketName}.`); -}); - -test.serial(`should remove a default user from a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}`, cwd); - t.is(output, `Removed user ${userEmail} from bucket ${bucketName}.`); -}); - -test.serial(`should print a user's acl for a file`, async (t) => { - await bucket.file(fileName).acl.readers.addUser(userEmail); - const output = await tools.runAsync(`${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}`, cwd); - t.is(output, `READER: user-${userEmail}`); - await bucket.file(fileName).acl.readers.deleteUser(userEmail); -}); - -test.serial(`should add a user as an owner on a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}`, cwd); - t.is(output, `Added user ${userEmail} as an owner on file ${fileName}.`); -}); - -test.serial(`should remove a user from a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}`, cwd); - t.is(output, `Removed user ${userEmail} from file ${fileName}.`); -}); diff --git a/storage/system-test/buckets.test.js b/storage/system-test/buckets.test.js deleted file mode 100644 index f017b2fec55..00000000000 --- a/storage/system-test/buckets.test.js +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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 test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const bucket = storage.bucket(bucketName); -const cmd = `node buckets.js`; - -test.before(tools.checkCredentials); -test.after.always(async () => { - try { - await bucket.delete(); - } catch (err) {} // ignore error -}); - -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); - -test.serial(`should create a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} create ${bucketName}`, cwd); - t.is(output, `Bucket ${bucketName} created.`); - const [exists] = await bucket.exists(); - t.true(exists); -}); - -test.serial(`should list buckets`, async (t) => { - t.plan(0); - await tools.tryTest(async (assert) => { - const output = await tools.runAsync(`${cmd} list`, cwd); - assert(output.includes(`Buckets:`)); - assert(output.includes(bucketName)); - }).start(); -}); - -test.serial(`should delete a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} delete ${bucketName}`, cwd); - t.is(output, `Bucket ${bucketName} deleted.`); - const [exists] = await bucket.exists(); - t.false(exists); -}); diff --git a/storage/system-test/encryption.test.js b/storage/system-test/encryption.test.js deleted file mode 100644 index dc1419816ec..00000000000 --- a/storage/system-test/encryption.test.js +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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 fs = require('fs'); -const path = require('path'); -const storage = require('@google-cloud/storage')(); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require('uuid'); - -const cwd = path.join(__dirname, `..`); -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const bucket = storage.bucket(bucketName); -const cmd = `node encryption.js`; - -const fileName = `test.txt`; -const filePath = path.join(__dirname, `../resources`, fileName); -const downloadFilePath = path.join(__dirname, `../resources/downloaded.txt`); - -let key; - -test.before(tools.checkCredentials); -test.before(async () => { - await bucket.create(bucketName); -}); - -test.after.always(async () => { - try { - // Delete the downloaded file - fs.unlinkSync(downloadFilePath); - } catch (err) { - console.log(err); - } - // Try deleting all files twice, just to make sure - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.delete(); - } catch (err) {} // ignore error -}); - -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); - -test.serial(`should generate a key`, async (t) => { - const output = await tools.runAsync(`${cmd} generate-encryption-key`, cwd); - t.true(output.includes(`Base 64 encoded encryption key:`)); - const test = /^Base 64 encoded encryption key: (.+)$/; - key = output.match(test)[1]; -}); - -test.serial(`should upload a file`, async (t) => { - const output = await tools.runAsync(`${cmd} upload ${bucketName} ${filePath} ${fileName} ${key}`, cwd); - t.is(output, `File ${filePath} uploaded to gs://${bucketName}/${fileName}.`); - const [exists] = await bucket.file(fileName).exists(); - t.true(exists); -}); - -test.serial(`should download a file`, async (t) => { - const output = await tools.runAsync(`${cmd} download ${bucketName} ${fileName} ${downloadFilePath} ${key}`, cwd); - t.is(output, `File ${fileName} downloaded to ${downloadFilePath}.`); - t.notThrows(() => { - fs.statSync(downloadFilePath); - }); -}); - -test.serial(`should rotate keys`, (t) => { - t.throws(() => { - tools.run(`${cmd} rotate ${bucketName} ${fileName} ${key} ${key}`, cwd); - }, Error, `This is currently not available using the Cloud Client Library.`); -}); diff --git a/storage/system-test/files.test.js b/storage/system-test/files.test.js deleted file mode 100644 index b8a2d199d61..00000000000 --- a/storage/system-test/files.test.js +++ /dev/null @@ -1,130 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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 fs = require(`fs`); -const path = require(`path`); -const storage = require(`@google-cloud/storage`)(); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const bucket = storage.bucket(bucketName); -const fileName = `test.txt`; -const movedFileName = `test2.txt`; -const copiedFileName = `test3.txt`; -const filePath = path.join(__dirname, `../resources`, fileName); -const downloadFilePath = path.join(__dirname, `../resources/downloaded.txt`); -const cmd = `node files.js`; - -test.before(tools.checkCredentials); -test.before(async () => { - await bucket.create(); -}); - -test.after.always(async () => { - try { - fs.unlinkSync(downloadFilePath); - } catch (err) { - console.log(err); - } - // Try deleting all files twice, just to make sure - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.delete(); - } catch (err) {} // ignore error -}); - -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); - -test.serial(`should upload a file`, async (t) => { - const output = await tools.runAsync(`${cmd} upload ${bucketName} ${filePath}`, cwd); - t.is(output, `${filePath} uploaded to ${bucketName}.`); - const [exists] = await bucket.file(fileName).exists(); - t.true(exists); -}); - -test.serial(`should download a file`, async (t) => { - const output = await tools.runAsync(`${cmd} download ${bucketName} ${fileName} ${downloadFilePath}`, cwd); - t.is(output, `gs://${bucketName}/${fileName} downloaded to ${downloadFilePath}.`); - t.notThrows(() => fs.statSync(downloadFilePath)); -}); - -test.serial(`should move a file`, async (t) => { - const output = await tools.runAsync(`${cmd} move ${bucketName} ${fileName} ${movedFileName}`, cwd); - t.is(output, `gs://${bucketName}/${fileName} moved to gs://${bucketName}/${movedFileName}.`); - const [exists] = await bucket.file(movedFileName).exists(); - t.true(exists); -}); - -test.serial(`should copy a file`, async (t) => { - const output = await tools.runAsync(`${cmd} copy ${bucketName} ${movedFileName} ${bucketName} ${copiedFileName}`, cwd); - t.is(output, `gs://${bucketName}/${movedFileName} copied to gs://${bucketName}/${copiedFileName}.`); - const [exists] = await bucket.file(copiedFileName).exists(); - t.true(exists); -}); - -test.serial(`should list files`, async (t) => { - t.plan(0); - await tools.tryTest(async (assert) => { - const output = await tools.runAsync(`${cmd} list ${bucketName}`, cwd); - assert(output.includes(`Files:`)); - assert(output.includes(movedFileName)); - assert(output.includes(copiedFileName)); - }).start(); -}); - -test.serial(`should list files by a prefix`, async (t) => { - let output = await tools.runAsync(`${cmd} list ${bucketName} test "/"`, cwd); - t.true(output.includes(`Files:`)); - t.true(output.includes(movedFileName)); - t.true(output.includes(copiedFileName)); - output = await tools.runAsync(`${cmd} list ${bucketName} foo`, cwd); - t.true(output.includes(`Files:`)); - t.false(output.includes(movedFileName)); - t.false(output.includes(copiedFileName)); -}); - -test.serial(`should make a file public`, async (t) => { - const output = await tools.runAsync(`${cmd} make-public ${bucketName} ${copiedFileName}`, cwd); - t.is(output, `gs://${bucketName}/${copiedFileName} is now public.`); -}); - -test.serial(`should generate a signed URL for a file`, async (t) => { - const output = await tools.runAsync(`${cmd} generate-signed-url ${bucketName} ${copiedFileName}`, cwd); - t.true(output.includes(`The signed url for ${copiedFileName} is `)); -}); - -test.serial(`should get metadata for a file`, async (t) => { - const output = await tools.runAsync(`${cmd} get-metadata ${bucketName} ${copiedFileName}`, cwd); - t.true(output.includes(`File: ${copiedFileName}`)); - t.true(output.includes(`Bucket: ${bucketName}`)); -}); - -test.serial(`should delete a file`, async (t) => { - const output = await tools.runAsync(`${cmd} delete ${bucketName} ${copiedFileName}`, cwd); - t.is(output, `gs://${bucketName}/${copiedFileName} deleted.`); - const [exists] = await bucket.file(copiedFileName).exists(); - t.false(exists); -}); diff --git a/storage/system-test/iam.test.js b/storage/system-test/iam.test.js deleted file mode 100644 index e2485ae0748..00000000000 --- a/storage/system-test/iam.test.js +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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 test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const bucket = storage.bucket(bucketName); -const userEmail = `test@example.com`; -const cmd = `node iam.js`; -const roleName = `roles/storage.objectViewer`; - -test.before(tools.checkCredentials); -test.before(async (t) => { - await bucket.create(); -}); - -test.after.always(async (t) => { - try { - await bucket.delete(); - } catch (err) {} // ignore error -}); - -test.serial(`should add multiple members to a role on a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} add-members ${bucketName} ${roleName} "user:${userEmail}"`, cwd); - t.true(output.includes(`Added the following member(s) with role ${roleName} to ${bucketName}:`)); - t.true(output.includes(`user:${userEmail}`)); -}); - -test.serial(`should list members of a role on a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} view-members ${bucketName} "user:${userEmail}"`, cwd); - t.true(output.includes(`Roles for bucket ${bucketName}:`)); - t.true(output.includes(`Role: ${roleName}`)); - t.true(output.includes(`Members:`)); - t.true(output.includes(`user:${userEmail}`)); -}); - -test.serial(`should remove multiple members from a role on a bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} remove-members ${bucketName} ${roleName} "user:${userEmail}"`, cwd); - t.true(output.includes(`Removed the following member(s) with role ${roleName} from ${bucketName}:`)); - t.true(output.includes(`user:${userEmail}`)); -}); diff --git a/storage/system-test/quickstart.test.js b/storage/system-test/quickstart.test.js deleted file mode 100644 index 96f0e23e335..00000000000 --- a/storage/system-test/quickstart.test.js +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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 proxyquire = require(`proxyquire`).noPreserveCache(); -const sinon = require(`sinon`); -const storage = proxyquire(`@google-cloud/storage`, {})(); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const bucket = storage.bucket(bucketName); - -test.before(tools.stubConsole); -test.after.always(async () => { - tools.restoreConsole(); - try { - await bucket.delete(); - } catch (err) {} // ignore error -}); - -test.cb(`should create a bucket`, (t) => { - const expectedBucketName = `my-new-bucket`; - - const storageMock = { - createBucket: (_bucketName) => { - t.is(_bucketName, expectedBucketName); - - return bucket.create() - .then(([bucket]) => { - t.not(bucket, undefined); - t.is(bucket.name, bucketName); - - setTimeout(() => { - try { - t.true(console.log.calledOnce); - t.deepEqual(console.log.firstCall.args, [`Bucket ${expectedBucketName} created.`]); - t.end(); - } catch (err) { - t.end(err); - } - }, 200); - - return [bucket]; - }); - } - }; - - proxyquire(`../quickstart`, { - '@google-cloud/storage': sinon.stub().returns(storageMock) - }); -}); diff --git a/storage/system-test/requesterPays.test.js b/storage/system-test/requesterPays.test.js deleted file mode 100644 index 015ca9737da..00000000000 --- a/storage/system-test/requesterPays.test.js +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * 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 fs = require(`fs`); -const path = require(`path`); -const storage = require(`@google-cloud/storage`)(); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); -const uuid = require(`uuid`); - -const cwd = path.join(__dirname, `..`); -const cmd = `node requesterPays.js`; -const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`; -const fileName = `test.txt`; -const bucket = storage.bucket(bucketName); - -const uploadFilePath = path.join(cwd, `resources`, fileName); -const downloadFilePath = path.join(__dirname, `test_${uuid.v4()}.txt`); - -test.before(async () => { - tools.checkCredentials(); - await bucket.create(); - - // Upload a test file (to download later) - await bucket.upload(uploadFilePath); -}); -test.after.always(async () => { - try { - fs.unlinkSync(downloadFilePath); - } catch (err) { - console.log(err); - } - // Try deleting all files twice, just to make sure - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.deleteFiles({ force: true }); - } catch (err) {} // ignore error - try { - await bucket.delete(); - } catch (err) {} // ignore error -}); - -test.serial(`should fetch requester-pays status on a default bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} get-status ${bucketName}`, cwd); - t.is(output, `Requester-pays requests are disabled for bucket ${bucketName}.`); -}); - -test.serial(`should enable requester-pays requests`, async (t) => { - const output = await tools.runAsync(`${cmd} enable ${bucketName}`, cwd); - t.is(output, `Requester-pays requests have been enabled for bucket ${bucketName}.`); -}); - -test.serial(`should fetch requester-pays status on a modified bucket`, async (t) => { - const output = await tools.runAsync(`${cmd} get-status ${bucketName}`, cwd); - t.is(output, `Requester-pays requests are enabled for bucket ${bucketName}.`); -}); - -test.serial(`should download a file using requester-pays requests`, async (t) => { - const output = await tools.runAsync(`${cmd} download ${bucketName} ${fileName} ${downloadFilePath}`, cwd); - t.is(output, `gs://${bucketName}/${fileName} downloaded to ${downloadFilePath} using requester-pays requests.`); - await t.notThrows(() => fs.statSync(downloadFilePath)); -}); - -test.serial(`should disable requester-pays requests`, async (t) => { - const output = await tools.runAsync(`${cmd} disable ${bucketName}`, cwd); - t.is(output, `Requester-pays requests have been disabled for bucket ${bucketName}.`); -}); - -test.serial(`should error on requester-pays requests if they are disabled`, async (t) => { - const result = await tools.runAsyncWithIO(`${cmd} download ${bucketName} ${fileName} ${downloadFilePath}`, cwd); - t.truthy(result.stderr); - t.regex(result.stderr, /User project prohibited for non requester pays bucket/); -});