Skip to content

Commit 2258efc

Browse files
authored
Merge branch 'main' into buckets-storage-migration
2 parents aea8df5 + 5987eab commit 2258efc

11 files changed

Lines changed: 787 additions & 0 deletions

storage/createNotification.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 files 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+
const uuid = require('uuid');
25+
26+
function main(
27+
bucketName = 'my-bucket',
28+
topic = `nodejs-storage-samples-${uuid.v4()}`
29+
) {
30+
// [START storage_create_bucket_notifications]
31+
/**
32+
* TODO(developer): Uncomment the following lines before running the sample.
33+
*/
34+
// The ID of your GCS bucket
35+
// const bucketName = 'your-unique-bucket-name';
36+
37+
// The name of a topic
38+
// const topic = 'my-topic';
39+
40+
// Imports the Google Cloud client library
41+
const {Storage} = require('@google-cloud/storage');
42+
43+
// Creates a client
44+
const storage = new Storage();
45+
46+
async function createNotification() {
47+
try {
48+
// Creates a notification
49+
await storage.bucket(bucketName).createNotification(topic);
50+
51+
console.log('Notification subscription created.');
52+
} catch (error) {
53+
console.error(
54+
'Error executing create notification:',
55+
error.message || error
56+
);
57+
}
58+
}
59+
60+
createNotification();
61+
// [END storage_create_bucket_notifications]
62+
}
63+
main(...process.argv.slice(2));

storage/deleteNotification.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 files 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', notificationId = '1') {
26+
// [START storage_delete_bucket_notification]
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+
// The ID of the notification
34+
// const notificationId = '1';
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 deleteNotification() {
43+
try {
44+
// Deletes the notification from the bucket
45+
await storage.bucket(bucketName).notification(notificationId).delete();
46+
47+
console.log(`Notification ${notificationId} deleted.`);
48+
} catch (error) {
49+
console.error(
50+
'Error executing delete notification:',
51+
error.message || error
52+
);
53+
}
54+
}
55+
56+
deleteNotification();
57+
// [END storage_delete_bucket_notification]
58+
}
59+
main(...process.argv.slice(2));

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));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 files 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', notificationId = '1') {
26+
// [START storage_print_pubsub_bucket_notification]
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+
// The ID of the notification
34+
// const notificationId = '1';
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 getMetadata() {
43+
try {
44+
// Get the notification metadata
45+
const [metadata] = await storage
46+
.bucket(bucketName)
47+
.notification(notificationId)
48+
.getMetadata();
49+
50+
console.log(`ID: ${metadata.id}`);
51+
console.log(`Topic: ${metadata.topic}`);
52+
console.log(`Event Types: ${metadata.event_types}`);
53+
console.log(`Custom Attributes: ${metadata.custom_attributes}`);
54+
console.log(`Payload Format: ${metadata.payload_format}`);
55+
console.log(`Object Name Prefix: ${metadata.object_name_prefix}`);
56+
console.log(`Etag: ${metadata.etag}`);
57+
console.log(`Self Link: ${metadata.selfLink}`);
58+
console.log(`Kind: ${metadata.kind}`);
59+
} catch (error) {
60+
console.error('Error executing get metadata:', error.message || error);
61+
}
62+
}
63+
64+
getMetadata();
65+
// [END storage_print_pubsub_bucket_notification]
66+
}
67+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)