node.js - GCP 云存储 : What is the difference between bucket. 上传和文件保存方法?

标签 node.js google-cloud-platform google-cloud-storage

上传文件有更好的选择吗?

在我的情况下,我需要上传很多小文件(pdf 或文本),我必须决定最佳选择,但这两种方法之间有区别吗?

这里有两个直接取自文档的例子

保存方法:(Docs)

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');

const file = myBucket.file('my-file');
const contents = 'This is the contents of the file.';

file.save(contents, function(err) {
  if (!err) {
    // File written successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
file.save(contents).then(function() {});

上传方式:(Docs)

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('albums');

//-
// Upload a file from a local path.
//-
bucket.upload('/local/path/image.png', function(err, file, apiResponse) {
  // Your bucket now contains:
  // - "image.png" (with the contents of `/local/path/image.png')

  // `file` is an instance of a File object that refers to your new file.
});

最佳答案

从本质上讲,这两个函数做同样的事情。他们将文件上传到存储桶。一个只是 bucket 上的一个函数,另一个是 file 上的一个函数。它们最终都调用了 file.createWriteStream,因此它们也具有相同的性能。

这些函数在上传类型方面表现不同。 file.save 将默认为可恢复上传,除非您另有指定(您可以将 SaveOptions 上的 resumable bool 值设置为 false)。如果文件小于 5MB,bucket.upload 将执行分段上传,否则执行可续传上传。对于 bucket.upload,您可以通过修改 UploadOptions 上的 resumable bool 值来强制进行可恢复或分段上传。

请注意,在即将发布的主要版本 (https://github.com/googleapis/nodejs-storage/pull/1876) 中,此行为将统一。无论文件大小如何,这两个功能都将默认为可恢复上传。行为将相同。

对于小文件,建议分段上传。

关于node.js - GCP 云存储 : What is the difference between bucket. 上传和文件保存方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71953136/

相关文章:

google-cloud-platform - 我将我的 Google Cloud 免费套餐升级为付费帐户,如何降级?

javascript - 如何找出函数中的模块路径

google-cloud-platform - 如何在 Terraform 中顺序创建 GCP SQL 数据库?

docker - 公共(public) GKE 集群中的 pod 无法访问互联网

google-app-engine - Google App Engine - 保留以前版本的静态文件

java - 如何从 Java 中的 Cloud Function 触发 Cloud Dataflow 管道作业?

json - npm 中忽略了 devDependency?

python - 如何保证服务器身份?

node.js - Webpack/Express - 服务器找不到环境变量

android - 获取存储在 Google Cloud Storage 中给定子目录中的所有对象