设置好代理
1.搭建nodejs测试项目
mkdir hello-express
cd hello-express/
npm init
yarn add express
2.添加
yarn add @google-cloud/storage
npm start
gcloud init
gcloud auth application-default login
修改后的代码
// Imports the Google Cloud client library
const { Storage } = require(’@google-cloud/storage’);
const path = require(‘path’);
const config=require(’./google-cloud-json.json’)
console.log(config)
// Creates a client
const storage = new Storage(config);
/**
- TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = ‘Name of a bucket, e.g. my-bucket’;
const filename = path.join(__dirname, ‘/file.txt’);
const bucketName = ‘test-69f62.appspot.com’;
var test = () => {
// Uploads a local file to the bucket
storage.bucket(bucketName).upload(filename, {
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: true,
// By setting the option `destination`, you can change the name of the
// object you are uploading to a bucket.
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
},
}).then(
res => {
console.log(res)
console.log(`${filename} uploaded to ${bucketName}.`);
}
).catch(e => {
console.log(e)
});
}
test();
google安装步骤
https://cloud.google.com/nodejs/