Skip to content
Draft
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
66 changes: 66 additions & 0 deletions storage/addBucketLabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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';

// sample-metadata:
// title: Storage Add Bucket Label.
// description: Adds bucket label.
// usage: node addBucketLabel.js <BUCKET_NAME> <LABEL_KEY> <LABEL_VALUE>

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

// The key of the label to add
// const labelKey = 'label-key-to-add';

// The value of the label to add
// const labelValue = 'label-value-to-add';

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

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

const labels = {
[labelKey]: labelValue,
};

async function addBucketLabel() {
try {
await storage.bucket(bucketName).setMetadata({labels});
console.log(`Added label to bucket ${bucketName}`);
} catch (error) {
console.error(
'Error executing add bucket label:',
error.message || error
);
}
}

addBucketLabel();
// [END storage_add_bucket_label]
}

main(...process.argv.slice(2));
70 changes: 70 additions & 0 deletions storage/addBucketWebsiteConfiguration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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: Bucket Website Configuration.
// description: Bucket Website Configuration.
// usage: node addBucketWebsiteConfiguration.js <BUCKET_NAME> <MAIN_PAGE_SUFFIX> <NOT_FOUND_PAGE>

function main(
bucketName = 'my-bucket',
mainPageSuffix = 'http://example.com',
notFoundPage = 'http://example.com/404.html'
) {
// [START storage_define_bucket_website_configuration]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The name of the main page
// const mainPageSuffix = 'http://example.com';

// The Name of a 404 page
// const notFoundPage = 'http://example.com/404.html';

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

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

async function addBucketWebsiteConfiguration() {
try {
await storage.bucket(bucketName).setMetadata({
website: {
mainPageSuffix,
notFoundPage,
},
});

console.log(
`Static website bucket ${bucketName} is set up to use ${mainPageSuffix} as the index page and ${notFoundPage} as the 404 page`
);
} catch (error) {
console.error(
'Error executing add bucket website configuration:',
error.message || error
);
}
}

addBucketWebsiteConfiguration();
// [END storage_define_bucket_website_configuration]
}

main(...process.argv.slice(2));
53 changes: 53 additions & 0 deletions storage/bucketMetadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019 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 Bucket Metadata.
// description: Get bucket metadata.
// usage: node bucketMetadata.js <BUCKET_NAME>

function main(bucketName = 'my-bucket') {
// [START storage_get_bucket_metadata]
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

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

async function getBucketMetadata() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// Get Bucket Metadata
try {
const [metadata] = await storage.bucket(bucketName).getMetadata();

console.log(JSON.stringify(metadata, null, 2));
} catch (error) {
console.error(
'Error executing get bucket metadata:',
error.message || error
);
}
}
// [END storage_get_bucket_metadata]
getBucketMetadata();
}

main(...process.argv.slice(2));
58 changes: 58 additions & 0 deletions storage/changeDefaultStorageClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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';

// sample-metadata:
// title: Change Bucket's Default Storage Class.
// description: Change Bucket's Default Storage Class.
// usage: node changeDefaultStorageClass.js <BUCKET_NAME> <CLASS_NAME>

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

// The name of a storage class
// See the StorageClass documentation for other valid storage classes:
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
// const storageClass = 'coldline';

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

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

async function changeDefaultStorageClass() {
try {
await storage.bucket(bucketName).setStorageClass(storageClass);

console.log(`${bucketName} has been set to ${storageClass}`);
} catch (error) {
console.error(
'Error executing change default storage class:',
error.message || error
);
}
}

changeDefaultStorageClass();
// [END storage_change_default_storage_class]
}

main(...process.argv.slice(2));
83 changes: 83 additions & 0 deletions storage/configureBucketCors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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';

// sample-metadata:
// title: Storage Configure Bucket Cors.
// description: Configures bucket cors.
// usage: node configureBucketCors.js <BUCKET_NAME> <MAX_AGE_SECONDS> <METHOD> <ORIGIN> <RESPONSE_HEADER>

function main(
bucketName = 'my-bucket',
maxAgeSeconds = 3600,
method = 'POST',
origin = 'http://example.appspot.com',
responseHeader = 'content-type'
) {
// [START storage_cors_configuration]
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

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

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The origin for this CORS config to allow requests from
// const origin = 'http://example.appspot.com';

// The response header to share across origins
// const responseHeader = 'Content-Type';

// The maximum amount of time the browser can make requests before it must
// repeat preflighted requests
// const maxAgeSeconds = 3600;

// The name of the method
// See the HttpMethod documentation for other HTTP methods available:
// https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/urlfetch/HTTPMethod
// const method = 'GET';

async function configureBucketCors() {
try {
await storage.bucket(bucketName).setCorsConfiguration([
{
maxAgeSeconds,
method: [method],
origin: [origin],
responseHeader: [responseHeader],
},
]);

console.log(`Bucket ${bucketName} was updated with a CORS config
to allow ${method} requests from ${origin} sharing
${responseHeader} responses across origins`);
} catch (error) {
console.error(
'Error executing configure bucket cors:',
error.message || error
);
}
}

configureBucketCors();
// [END storage_cors_configuration]
}

main(...process.argv.slice(2));
Loading
Loading