如何在文件上传到 Google 云存储之前将其标记为私有?
How to mark a file private before it's uploaded to Google Cloud Storage?
我正在使用 @google-cloud/storage 包并生成签名 url 来上传这样的文件:
const path = require("path");
const { Storage } = require("@google-cloud/storage");
const GOOGLE_CLOUD_KEYFILE = path.resolve(
__dirname + "/../gcloud_media_access.json"
);
const storage = new Storage({
keyFilename: GOOGLE_CLOUD_KEYFILE,
});
exports.uploadUrlGCloud = async (bucketName, key, isPrivate = false) => {
let bucket = storage.bucket(bucketName);
let file = bucket.file(key);
const options = {
version: "v4",
action: "write",
expires: Date.now() + 15 * 60 * 1000 // 15 minutes
};
let signedUrl = (await file.getSignedUrl(options))[0];
if(isPrivate){
await file.makePrivate({strict: true});
}
return signedUrl;
};
但是当我这样调用这个函数时:
const url = await uploadUrlGCloud(bucket, key, true);
我收到这样的 404 api 错误:
ApiError: No such object: testbucket/account/upload/4aac0fb0-92dd-11eb-8723-6b3ad09f80fa_demo.jpg
我想问的是有没有办法生成私有的signedUrl?在上传文件之前,我想将其标记为私有并阻止 public 访问。
编辑:
我上传了一个文件到创建的签名URL,并再次对上传的文件做了makePrivate
。这次我没有收到任何错误。然而,当我再次检查文件时,我发现仍然是 public.
这是我尝试将文件设为私有的函数:
const makeFilePrivate = async (bucketName, key) => {
return new Promise((resolve, reject) => {
let bucket = storage.bucket(bucketName);
let file = bucket.file(key);
try {
file.makePrivate({strict: true}, err => {
if(!err) {
resolve(file.isPublic());
} else
reject(err);
})
} catch (err) {
reject(err);
}
})
};
console.log(await makeFilePrivate(bucket, remotePath));
// True
我正在使用 @google-cloud/storage 包并生成签名 url 来上传这样的文件:
const path = require("path");
const { Storage } = require("@google-cloud/storage");
const GOOGLE_CLOUD_KEYFILE = path.resolve(
__dirname + "/../gcloud_media_access.json"
);
const storage = new Storage({
keyFilename: GOOGLE_CLOUD_KEYFILE,
});
exports.uploadUrlGCloud = async (bucketName, key, isPrivate = false) => {
let bucket = storage.bucket(bucketName);
let file = bucket.file(key);
const options = {
version: "v4",
action: "write",
expires: Date.now() + 15 * 60 * 1000 // 15 minutes
};
let signedUrl = (await file.getSignedUrl(options))[0];
if(isPrivate){
await file.makePrivate({strict: true});
}
return signedUrl;
};
但是当我这样调用这个函数时:
const url = await uploadUrlGCloud(bucket, key, true);
我收到这样的 404 api 错误:
ApiError: No such object: testbucket/account/upload/4aac0fb0-92dd-11eb-8723-6b3ad09f80fa_demo.jpg
我想问的是有没有办法生成私有的signedUrl?在上传文件之前,我想将其标记为私有并阻止 public 访问。
编辑:
我上传了一个文件到创建的签名URL,并再次对上传的文件做了makePrivate
。这次我没有收到任何错误。然而,当我再次检查文件时,我发现仍然是 public.
这是我尝试将文件设为私有的函数:
const makeFilePrivate = async (bucketName, key) => {
return new Promise((resolve, reject) => {
let bucket = storage.bucket(bucketName);
let file = bucket.file(key);
try {
file.makePrivate({strict: true}, err => {
if(!err) {
resolve(file.isPublic());
} else
reject(err);
})
} catch (err) {
reject(err);
}
})
};
console.log(await makeFilePrivate(bucket, remotePath));
// True