使用 multer 和 gcloud 上传文件时未设置目的地
Destination not being set while uploading files using multer and gcloud
我正在尝试上传我 google 存储桶中特定文件夹中的文件。我使用 Google documentations
编写代码
app.js
const multerMid = multer({
storage: multer.memoryStorage(),
limits: {
// no larger than 5mb.
fileSize: 5 * 1024 * 1024,
},
});
app.disable("x-powered-by");
app.use(
multerMid.fields([
{ name: "avatar", maxCount: 1 },
{ name: "gallery", maxCount: 8 },
])
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/uploads", async (req, res, next) => {
try {
const myFile = req.files;
console.log(myFile);
const imageUrl = await uploadImage(myFile.gallery[0]);
res.status(200).json({
message: "Upload was successful",
data: imageUrl,
});
} catch (error) {
next(error);
}
});
上传图片功能
const uploadImage = (file) =>
new Promise((resolve, reject) => {
const { originalname, buffer } = file;
const blob = bucket.file(originalname.replace(/\s+/g, "_"));
const blobStream = blob.createWriteStream({
destination: "documents/image.png",
resumable: false,
});
blobStream
.on("finish", () => {
const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;
resolve(publicUrl);
})
.on("error", () => {
reject(`Unable to upload image, something went wrong`);
})
.end(buffer);
});
在这里,我将目标设置为 documents/image.png,但文件仍存储在存储桶根目录中,而不是文档文件夹中。如何让文件存储在存储桶中的特定文件夹中?
createWriteStream
在其 configuration
选项中没有名为 destination
的 property
!
https://googleapis.dev/nodejs/storage/latest/global.html#CreateWriteStreamOptions
有uploadOptions
对象:
https://googleapis.dev/nodejs/storage/latest/global.html#UploadOptions
解决方案:您应该使用 upload
便捷方法(包装 createWriteStream
)并将 uploadOptions
对象作为 destionation
属性 传递给它!
https://googleapis.dev/nodejs/storage/latest/Bucket.html#upload
我已经找到了将文件存储在特定子目录中的方法 google 存储 bucket.Google 将文件存储在其存储桶中,其名称也包含它的路径。例如,如果图像名称 xyz.jpg
存储在 mnp
存储桶中的 abc
文件夹中。文件名将是 mnp/abc/xyz.jpg
。因此,要将文件保存在特定目录中,我们只需在原始文件名中添加路径即可。
const uploadImage = (file) =>
new Promise((resolve, reject) => {
const { originalname, buffer } = file;
const blob =
bucket.file(originalname.replace(originalname,"document/"+originalname));
const blobStream = blob.createWriteStream({
resumable: false,
});
blobStream
.on("finish", () => {
const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;
resolve(publicUrl);
})
.on("error", () => {
reject(`Unable to upload image, something went wrong`);
})
.end(buffer);
});
我正在尝试上传我 google 存储桶中特定文件夹中的文件。我使用 Google documentations
编写代码app.js
const multerMid = multer({
storage: multer.memoryStorage(),
limits: {
// no larger than 5mb.
fileSize: 5 * 1024 * 1024,
},
});
app.disable("x-powered-by");
app.use(
multerMid.fields([
{ name: "avatar", maxCount: 1 },
{ name: "gallery", maxCount: 8 },
])
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/uploads", async (req, res, next) => {
try {
const myFile = req.files;
console.log(myFile);
const imageUrl = await uploadImage(myFile.gallery[0]);
res.status(200).json({
message: "Upload was successful",
data: imageUrl,
});
} catch (error) {
next(error);
}
});
上传图片功能
const uploadImage = (file) =>
new Promise((resolve, reject) => {
const { originalname, buffer } = file;
const blob = bucket.file(originalname.replace(/\s+/g, "_"));
const blobStream = blob.createWriteStream({
destination: "documents/image.png",
resumable: false,
});
blobStream
.on("finish", () => {
const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;
resolve(publicUrl);
})
.on("error", () => {
reject(`Unable to upload image, something went wrong`);
})
.end(buffer);
});
在这里,我将目标设置为 documents/image.png,但文件仍存储在存储桶根目录中,而不是文档文件夹中。如何让文件存储在存储桶中的特定文件夹中?
createWriteStream
在其 configuration
选项中没有名为 destination
的 property
!
https://googleapis.dev/nodejs/storage/latest/global.html#CreateWriteStreamOptions
有uploadOptions
对象:
https://googleapis.dev/nodejs/storage/latest/global.html#UploadOptions
解决方案:您应该使用 upload
便捷方法(包装 createWriteStream
)并将 uploadOptions
对象作为 destionation
属性 传递给它!
https://googleapis.dev/nodejs/storage/latest/Bucket.html#upload
我已经找到了将文件存储在特定子目录中的方法 google 存储 bucket.Google 将文件存储在其存储桶中,其名称也包含它的路径。例如,如果图像名称 xyz.jpg
存储在 mnp
存储桶中的 abc
文件夹中。文件名将是 mnp/abc/xyz.jpg
。因此,要将文件保存在特定目录中,我们只需在原始文件名中添加路径即可。
const uploadImage = (file) =>
new Promise((resolve, reject) => {
const { originalname, buffer } = file;
const blob =
bucket.file(originalname.replace(originalname,"document/"+originalname));
const blobStream = blob.createWriteStream({
resumable: false,
});
blobStream
.on("finish", () => {
const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;
resolve(publicUrl);
})
.on("error", () => {
reject(`Unable to upload image, something went wrong`);
})
.end(buffer);
});