Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions storage/disableRequesterPays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2020 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';

/**
* 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.
*/

function main(projectId, bucketName = 'my-bucket') {
// [START storage_disable_requester_pays]

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your Google Cloud project
// const projectId = 'your-project-id';

// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function disableRequesterPays() {
try {
// Disables requester-pays requests
await storage
.bucket(bucketName)
.disableRequesterPays({userProject: projectId});

console.log(
`Requester-pays requests have been disabled for bucket ${bucketName}`
);
} catch (error) {
console.error(
'Error executing disable requester pays:',
error.message || error
);
}
}

disableRequesterPays();
// [END storage_disable_requester_pays]
}
main(...process.argv.slice(2));
81 changes: 81 additions & 0 deletions storage/downloadFileUsingRequesterPays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2020 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';

/**
* 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.
*/

const uuid = require('uuid');
const path = require('path');

function main(
projectId = 'cloud-devrel-public-resources',
bucketName = `nodejs-storage-samples-${uuid.v4()}`,
srcFileName = 'test.txt',
destFileName = path.join(__dirname, `test_${uuid.v4()}.txt`)
) {
// [START storage_download_file_requester_pays]

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The project ID to bill
// const projectId = 'my-billable-project-id';

// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The ID of your GCS file
// const srcFileName = 'your-file-name';

// The path to which the file should be downloaded
// const destFileName = '/local/path/to/file.txt';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function downloadFileUsingRequesterPays() {
try {
const options = {
destination: destFileName,
userProject: projectId,
};

// Downloads the file
await storage.bucket(bucketName).file(srcFileName).download(options);

console.log(
`gs://${bucketName}/${srcFileName} downloaded to ${destFileName} using requester-pays requests`
);
} catch (error) {
console.error(
'Error executing download file using requester pays:',
error.message || error
);
}
}

downloadFileUsingRequesterPays();
// [END storage_download_file_requester_pays]
}
main(...process.argv.slice(2));
57 changes: 57 additions & 0 deletions storage/enableRequesterPays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 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';

/**
* 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.
*/

function main(bucketName = 'my-bucket') {
// [START storage_enable_requester_pays]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function enableRequesterPays() {
try {
await storage.bucket(bucketName).enableRequesterPays();

console.log(
`Requester-pays requests have been enabled for bucket ${bucketName}`
);
} catch (error) {
console.error(
'Error executing enable requester pays:',
error.message || error
);
}
}

enableRequesterPays();
// [END storage_enable_requester_pays]
}
main(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions storage/getRequesterPaysStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2020 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';

/**
* 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.
*/

function main(projectId, bucketName = 'my-bucket') {
// [START storage_get_requester_pays_status]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your Google Cloud project
// const projectId = 'your-project-id';

// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function getRequesterPaysStatus() {
try {
// Gets the requester-pays status of a bucket
const [metadata] = await storage
.bucket(bucketName)
.getMetadata({userProject: projectId});

let status;
if (metadata && metadata.billing && metadata.billing.requesterPays) {
status = 'enabled';
} else {
status = 'disabled';
}
console.log(
`Requester-pays requests are ${status} for bucket ${bucketName}.`
);
} catch (error) {
console.error(
'Error executing get requester pays status:',
error.message || error
);
}
}

getRequesterPaysStatus();
// [END storage_get_requester_pays_status]
}
main(...process.argv.slice(2));
55 changes: 55 additions & 0 deletions storage/getServiceAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2021 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: Storage Get Service Account.
// description: Get Service Account.
// usage: node getServiceAccount.js <PROJECT_ID>

function main(projectId = 'serviceAccountProjectId') {
// [START storage_get_service_account]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCP project
// const projectId = 'your-project-id';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage({
projectId,
});

async function getServiceAccount() {
try {
const [serviceAccount] = await storage.getServiceAccount();
console.log(
`The GCS service account for project ${projectId} is: ${serviceAccount.emailAddress}`
);
} catch (error) {
console.error(
'Error executing get service account:',
error.message || error
);
}
}

getServiceAccount();
// [END storage_get_service_account]
}
main(...process.argv.slice(2));
30 changes: 30 additions & 0 deletions storage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@google-cloud/storage-samples",
"description": "Samples for the Cloud Storage Client Library for Node.js.",
"license": "Apache-2.0",
"author": "Google Inc.",
"engines": {
"node": ">=12"
},
"repository": "googleapis/nodejs-storage",
"private": true,
"files": [
"*.js"
],
"scripts": {
"cleanup": "node scripts/cleanup",
"test": "mocha system-test/*.js --timeout 800000"
},
"dependencies": {
"@google-cloud/pubsub": "^4.0.0",
"@google-cloud/storage": "^7.19.0",
"node-fetch": "^2.6.7",
"uuid": "^8.0.0",
"yargs": "^16.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^8.0.0",
"p-limit": "^3.1.0"
}
}
1 change: 1 addition & 0 deletions storage/resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
downloaded.txt
2 changes: 2 additions & 0 deletions storage/resources/resourcesSub1/testSub1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Sub1
Hello World!
1 change: 1 addition & 0 deletions storage/resources/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
1 change: 1 addition & 0 deletions storage/resources/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World 2!
Loading
Loading