nodejs google-cloud/storage 各种坑

本文记录了在Node.js项目中集成和配置Google Cloud Storage的过程,包括创建项目、安装依赖、授权登录以及遇到的问题。通过示例代码展示了如何初始化Storage客户端,并提供了官方的安装步骤链接作为参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置好代理
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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值