将文件上传到 Google Cloud 的特定目录中 [英] Upload a file to Google Cloud, in a specific directory

查看:24
本文介绍了将文件上传到 Google Cloud 的特定目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Google Cloud 上的特定存储桶目录(例如 foo)中上传文件?

How to upload a file on Google Cloud, in a specific bucket directory (e.g. foo)?

"use strict";

const gcloud = require("gcloud");

const PROJECT_ID = "<project-id>";

let storage = gcloud.storage({
  projectId: PROJECT_ID,
  keyFilename: 'auth.json'
});

let bucket = storage.bucket(`${PROJECT_ID}.appspot.com`)
bucket.upload("1.jpg", (err, file) => {
    if (err) { return console.error(err); }
    let publicUrl = `https://firebasestorage.googleapis.com/v0/b/${PROJECT_ID}.appspot.com/o/${file.metadata.name}?alt=media`;
    console.log(publicUrl);
});

我试过了:

bucket.file("foo/1.jpg").upload("1.jpg", ...)

但是那里没有 upload 方法.

But there's no upload method there.

foo目录下的1.jpg如何发送?

在 Firebase 中,在客户端,我这样做:

In Firebase, on the client side, I do:

ref.child("foo").put(myFile);

推荐答案

bucket.upload("1.jpg", { destination: "YOUR_FOLDER_NAME_HERE/1.jpg" }, (err, file) => {
    //Do something...
});

这会将 1.jpg 放入 YOUR_FOLDER_NAME_HERE 文件夹中.

This will put 1.jpg in the YOUR_FOLDER_NAME_HERE-folder.

这里 是文档.顺便说一句, gcloud 已弃用,您应该使用 google-cloud 代替.

Here is the documentation. By the way, gcloud is deprecated and you should use google-cloud instead.

这篇关于将文件上传到 Google Cloud 的特定目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆