Skip to content

Commit 576f3d7

Browse files
committed
feat(storage): introduce core bucket samples and tests for migration
1 parent 3b66abe commit 576f3d7

42 files changed

Lines changed: 2663 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

storage/addBucketLabel.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
// sample-metadata:
18+
// title: Storage Add Bucket Label.
19+
// description: Adds bucket label.
20+
// usage: node addBucketLabel.js <BUCKET_NAME> <LABEL_KEY> <LABEL_VALUE>
21+
22+
function main(
23+
bucketName = 'my-bucket',
24+
labelKey = 'labelone',
25+
labelValue = 'labelonevalue'
26+
) {
27+
// [START storage_add_bucket_label]
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// The ID of your GCS bucket
32+
// const bucketName = 'your-unique-bucket-name';
33+
34+
// The key of the label to add
35+
// const labelKey = 'label-key-to-add';
36+
37+
// The value of the label to add
38+
// const labelValue = 'label-value-to-add';
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+
const labels = {
47+
[labelKey]: labelValue,
48+
};
49+
50+
async function addBucketLabel() {
51+
try {
52+
await storage.bucket(bucketName).setMetadata({labels});
53+
console.log(`Added label to bucket ${bucketName}`);
54+
} catch (error) {
55+
console.error(
56+
'Error executing add bucket label:',
57+
error.message || error
58+
);
59+
}
60+
}
61+
62+
addBucketLabel();
63+
// [END storage_add_bucket_label]
64+
}
65+
66+
main(...process.argv.slice(2));
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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: Bucket Website Configuration.
19+
// description: Bucket Website Configuration.
20+
// usage: node addBucketWebsiteConfiguration.js <BUCKET_NAME> <MAIN_PAGE_SUFFIX> <NOT_FOUND_PAGE>
21+
22+
function main(
23+
bucketName = 'my-bucket',
24+
mainPageSuffix = 'http://example.com',
25+
notFoundPage = 'http://example.com/404.html'
26+
) {
27+
// [START storage_define_bucket_website_configuration]
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// The ID of your GCS bucket
32+
// const bucketName = 'your-unique-bucket-name';
33+
34+
// The name of the main page
35+
// const mainPageSuffix = 'http://example.com';
36+
37+
// The Name of a 404 page
38+
// const notFoundPage = 'http://example.com/404.html';
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 addBucketWebsiteConfiguration() {
47+
try {
48+
await storage.bucket(bucketName).setMetadata({
49+
website: {
50+
mainPageSuffix,
51+
notFoundPage,
52+
},
53+
});
54+
55+
console.log(
56+
`Static website bucket ${bucketName} is set up to use ${mainPageSuffix} as the index page and ${notFoundPage} as the 404 page`
57+
);
58+
} catch (error) {
59+
console.error(
60+
'Error executing add bucket website configuration:',
61+
error.message || error
62+
);
63+
}
64+
}
65+
66+
addBucketWebsiteConfiguration();
67+
// [END storage_define_bucket_website_configuration]
68+
}
69+
70+
main(...process.argv.slice(2));

storage/bucketMetadata.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2019 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 Bucket Metadata.
19+
// description: Get bucket metadata.
20+
// usage: node bucketMetadata.js <BUCKET_NAME>
21+
22+
function main(bucketName = 'my-bucket') {
23+
// [START storage_get_bucket_metadata]
24+
// Imports the Google Cloud client library
25+
const {Storage} = require('@google-cloud/storage');
26+
27+
// Creates a client
28+
const storage = new Storage();
29+
30+
async function getBucketMetadata() {
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+
// Get Bucket Metadata
38+
try {
39+
const [metadata] = await storage.bucket(bucketName).getMetadata();
40+
41+
console.log(JSON.stringify(metadata, null, 2));
42+
} catch (error) {
43+
console.error(
44+
'Error executing get bucket metadata:',
45+
error.message || error
46+
);
47+
}
48+
}
49+
// [END storage_get_bucket_metadata]
50+
getBucketMetadata();
51+
}
52+
53+
main(...process.argv.slice(2));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// sample-metadata:
18+
// title: Change Bucket's Default Storage Class.
19+
// description: Change Bucket's Default Storage Class.
20+
// usage: node changeDefaultStorageClass.js <BUCKET_NAME> <CLASS_NAME>
21+
22+
function main(bucketName = 'my-bucket', storageClass = 'standard') {
23+
// [START storage_change_default_storage_class]
24+
/**
25+
* TODO(developer): Uncomment the following lines before running the sample.
26+
*/
27+
// The ID of your GCS bucket
28+
// const bucketName = 'your-unique-bucket-name';
29+
30+
// The name of a storage class
31+
// See the StorageClass documentation for other valid storage classes:
32+
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
33+
// const storageClass = 'coldline';
34+
35+
// Imports the Google Cloud client library
36+
const {Storage} = require('@google-cloud/storage');
37+
38+
// Creates a client
39+
const storage = new Storage();
40+
41+
async function changeDefaultStorageClass() {
42+
try {
43+
await storage.bucket(bucketName).setStorageClass(storageClass);
44+
45+
console.log(`${bucketName} has been set to ${storageClass}`);
46+
} catch (error) {
47+
console.error(
48+
'Error executing change default storage class:',
49+
error.message || error
50+
);
51+
}
52+
}
53+
54+
changeDefaultStorageClass();
55+
// [END storage_change_default_storage_class]
56+
}
57+
58+
main(...process.argv.slice(2));

storage/configureBucketCors.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
// sample-metadata:
18+
// title: Storage Configure Bucket Cors.
19+
// description: Configures bucket cors.
20+
// usage: node configureBucketCors.js <BUCKET_NAME> <MAX_AGE_SECONDS> <METHOD> <ORIGIN> <RESPONSE_HEADER>
21+
22+
function main(
23+
bucketName = 'my-bucket',
24+
maxAgeSeconds = 3600,
25+
method = 'POST',
26+
origin = 'http://example.appspot.com',
27+
responseHeader = 'content-type'
28+
) {
29+
// [START storage_cors_configuration]
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+
36+
/**
37+
* TODO(developer): Uncomment the following lines before running the sample.
38+
*/
39+
// The ID of your GCS bucket
40+
// const bucketName = 'your-unique-bucket-name';
41+
42+
// The origin for this CORS config to allow requests from
43+
// const origin = 'http://example.appspot.com';
44+
45+
// The response header to share across origins
46+
// const responseHeader = 'Content-Type';
47+
48+
// The maximum amount of time the browser can make requests before it must
49+
// repeat preflighted requests
50+
// const maxAgeSeconds = 3600;
51+
52+
// The name of the method
53+
// See the HttpMethod documentation for other HTTP methods available:
54+
// https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/urlfetch/HTTPMethod
55+
// const method = 'GET';
56+
57+
async function configureBucketCors() {
58+
try {
59+
await storage.bucket(bucketName).setCorsConfiguration([
60+
{
61+
maxAgeSeconds,
62+
method: [method],
63+
origin: [origin],
64+
responseHeader: [responseHeader],
65+
},
66+
]);
67+
68+
console.log(`Bucket ${bucketName} was updated with a CORS config
69+
to allow ${method} requests from ${origin} sharing
70+
${responseHeader} responses across origins`);
71+
} catch (error) {
72+
console.error(
73+
'Error executing configure bucket cors:',
74+
error.message || error
75+
);
76+
}
77+
}
78+
79+
configureBucketCors();
80+
// [END storage_cors_configuration]
81+
}
82+
83+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)