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

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

问题描述

如何在Google Cloud,特定存储区目录中(例如 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);
});

我试过了:

上传(1.jpg,...)

但没有 upload 方法。



如何发送 1.jpg foo 目录中?

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

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


解决方案

 上传(1.jpg,{destination:YOUR_FOLDER_NAME_HERE / 1.jpg},(err,file)=> {
//做某事...
});

这会将 1.jpg 放入 YOUR_FOLDER_NAME_HERE -folder。



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


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);
});

I tried:

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

But there's no upload method there.

How can I send 1.jpg in the foo directory?

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...
});

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

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

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

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