GCP云存储:桶有什么区别?上传和文件.拯救方法吗?



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

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

下面两个例子直接取自文档

保存方法:(文档)

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

上传方法:(文档)

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布尔值设置为false)。如果文件小于5MB,bucket.upload将执行多部分上传,否则将执行可恢复上传。对于bucket.upload,您可以通过修改UploadOptions上的resumable布尔值来强制可恢复或多部分上传。

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

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

最新更新