Skip to content

Commit aa83d93

Browse files
authored
feat(storage): migrate requester pays samples and tests (#4265)
* feat(storage): introduce requester pays samples and tests for migration * test(storage): fix requester pays system test
1 parent 3b66abe commit aa83d93

13 files changed

Lines changed: 516 additions & 0 deletions

storage/disableRequesterPays.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on buckets with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
function main(projectId, bucketName = 'my-bucket') {
26+
// [START storage_disable_requester_pays]
27+
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// The ID of your Google Cloud project
32+
// const projectId = 'your-project-id';
33+
34+
// The ID of your GCS bucket
35+
// const bucketName = 'your-unique-bucket-name';
36+
37+
// Imports the Google Cloud client library
38+
const {Storage} = require('@google-cloud/storage');
39+
40+
// Creates a client
41+
const storage = new Storage();
42+
43+
async function disableRequesterPays() {
44+
try {
45+
// Disables requester-pays requests
46+
await storage
47+
.bucket(bucketName)
48+
.disableRequesterPays({userProject: projectId});
49+
50+
console.log(
51+
`Requester-pays requests have been disabled for bucket ${bucketName}`
52+
);
53+
} catch (error) {
54+
console.error(
55+
'Error executing disable requester pays:',
56+
error.message || error
57+
);
58+
}
59+
}
60+
61+
disableRequesterPays();
62+
// [END storage_disable_requester_pays]
63+
}
64+
main(...process.argv.slice(2));
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on buckets with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
const uuid = require('uuid');
26+
const path = require('path');
27+
28+
function main(
29+
projectId = 'cloud-devrel-public-resources',
30+
bucketName = `nodejs-storage-samples-${uuid.v4()}`,
31+
srcFileName = 'test.txt',
32+
destFileName = path.join(__dirname, `test_${uuid.v4()}.txt`)
33+
) {
34+
// [START storage_download_file_requester_pays]
35+
36+
/**
37+
* TODO(developer): Uncomment the following lines before running the sample.
38+
*/
39+
// The project ID to bill
40+
// const projectId = 'my-billable-project-id';
41+
42+
// The ID of your GCS bucket
43+
// const bucketName = 'your-unique-bucket-name';
44+
45+
// The ID of your GCS file
46+
// const srcFileName = 'your-file-name';
47+
48+
// The path to which the file should be downloaded
49+
// const destFileName = '/local/path/to/file.txt';
50+
51+
// Imports the Google Cloud client library
52+
const {Storage} = require('@google-cloud/storage');
53+
54+
// Creates a client
55+
const storage = new Storage();
56+
57+
async function downloadFileUsingRequesterPays() {
58+
try {
59+
const options = {
60+
destination: destFileName,
61+
userProject: projectId,
62+
};
63+
64+
// Downloads the file
65+
await storage.bucket(bucketName).file(srcFileName).download(options);
66+
67+
console.log(
68+
`gs://${bucketName}/${srcFileName} downloaded to ${destFileName} using requester-pays requests`
69+
);
70+
} catch (error) {
71+
console.error(
72+
'Error executing download file using requester pays:',
73+
error.message || error
74+
);
75+
}
76+
}
77+
78+
downloadFileUsingRequesterPays();
79+
// [END storage_download_file_requester_pays]
80+
}
81+
main(...process.argv.slice(2));

storage/enableRequesterPays.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on buckets with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
function main(bucketName = 'my-bucket') {
26+
// [START storage_enable_requester_pays]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function enableRequesterPays() {
40+
try {
41+
await storage.bucket(bucketName).enableRequesterPays();
42+
43+
console.log(
44+
`Requester-pays requests have been enabled for bucket ${bucketName}`
45+
);
46+
} catch (error) {
47+
console.error(
48+
'Error executing enable requester pays:',
49+
error.message || error
50+
);
51+
}
52+
}
53+
54+
enableRequesterPays();
55+
// [END storage_enable_requester_pays]
56+
}
57+
main(...process.argv.slice(2));

storage/getRequesterPaysStatus.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to perform basic operations on buckets with
19+
* the Google Cloud Storage API.
20+
*
21+
* For more information, see the README.md under /storage and the documentation
22+
* at https://cloud.google.com/storage/docs.
23+
*/
24+
25+
function main(projectId, bucketName = 'my-bucket') {
26+
// [START storage_get_requester_pays_status]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your Google Cloud project
31+
// const projectId = 'your-project-id';
32+
33+
// The ID of your GCS bucket
34+
// const bucketName = 'your-unique-bucket-name';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
async function getRequesterPaysStatus() {
43+
try {
44+
// Gets the requester-pays status of a bucket
45+
const [metadata] = await storage
46+
.bucket(bucketName)
47+
.getMetadata({userProject: projectId});
48+
49+
let status;
50+
if (metadata && metadata.billing && metadata.billing.requesterPays) {
51+
status = 'enabled';
52+
} else {
53+
status = 'disabled';
54+
}
55+
console.log(
56+
`Requester-pays requests are ${status} for bucket ${bucketName}.`
57+
);
58+
} catch (error) {
59+
console.error(
60+
'Error executing get requester pays status:',
61+
error.message || error
62+
);
63+
}
64+
}
65+
66+
getRequesterPaysStatus();
67+
// [END storage_get_requester_pays_status]
68+
}
69+
main(...process.argv.slice(2));

storage/getServiceAccount.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// sample-metadata:
18+
// title: Storage Get Service Account.
19+
// description: Get Service Account.
20+
// usage: node getServiceAccount.js <PROJECT_ID>
21+
22+
function main(projectId = 'serviceAccountProjectId') {
23+
// [START storage_get_service_account]
24+
/**
25+
* TODO(developer): Uncomment the following lines before running the sample.
26+
*/
27+
// The ID of your GCP project
28+
// const projectId = 'your-project-id';
29+
30+
// Imports the Google Cloud client library
31+
const {Storage} = require('@google-cloud/storage');
32+
33+
// Creates a client
34+
const storage = new Storage({
35+
projectId,
36+
});
37+
38+
async function getServiceAccount() {
39+
try {
40+
const [serviceAccount] = await storage.getServiceAccount();
41+
console.log(
42+
`The GCS service account for project ${projectId} is: ${serviceAccount.emailAddress}`
43+
);
44+
} catch (error) {
45+
console.error(
46+
'Error executing get service account:',
47+
error.message || error
48+
);
49+
}
50+
}
51+
52+
getServiceAccount();
53+
// [END storage_get_service_account]
54+
}
55+
main(...process.argv.slice(2));

storage/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@google-cloud/storage-samples",
3+
"description": "Samples for the Cloud Storage Client Library for Node.js.",
4+
"license": "Apache-2.0",
5+
"author": "Google Inc.",
6+
"engines": {
7+
"node": ">=12"
8+
},
9+
"repository": "googleapis/nodejs-storage",
10+
"private": true,
11+
"files": [
12+
"*.js"
13+
],
14+
"scripts": {
15+
"cleanup": "node scripts/cleanup",
16+
"test": "mocha system-test/*.js --timeout 800000"
17+
},
18+
"dependencies": {
19+
"@google-cloud/pubsub": "^4.0.0",
20+
"@google-cloud/storage": "^7.19.0",
21+
"node-fetch": "^2.6.7",
22+
"uuid": "^8.0.0",
23+
"yargs": "^16.0.0"
24+
},
25+
"devDependencies": {
26+
"chai": "^4.2.0",
27+
"mocha": "^8.0.0",
28+
"p-limit": "^3.1.0"
29+
}
30+
}

storage/resources/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
downloaded.txt
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Sub1
2+
Hello World!

storage/resources/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!

storage/resources/test2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World 2!

0 commit comments

Comments
 (0)