[英]View and not download Google Cloud Storage files in browser
I'am working with NodeJs/ express to create a POST api endpoint to upload files to google cloud storage and returning a public URL like this : https://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]我正在与 NodeJs/express 合作创建一个 POST api 端点以将文件上传到谷歌云存储并返回一个公共 URL,如下所示: https ://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME ]
When the upload is done, I get the URL and when I open it , the file is downloaded directly (image, pdf etc...)上传完成后,我得到了 URL,当我打开它时,直接下载文件(图像、pdf 等...)
Is there a way to view and open it in the browser ?有没有办法在浏览器中查看和打开它?
here is my upload function :这是我的上传功能:
const uploadImage = (file) => new Promise((resolve, reject) => {
const { originalname, buffer } = file
const blob = bucket.file(originalname.replace(/ /g, "_"))
const blobStream = blob.createWriteStream({
resumable: false
})
blobStream.on('finish', () => {
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
)
resolve(publicUrl)
})
.on('error', () => {
reject(`Unable to upload image, something went wrong`)
})
.end(buffer)
})
Thanks谢谢
If the URL looks like the code above, https://storage.googleapis.com/bucketName/objectName
, a browser should be able to view it directly, so long as a few conditions are in place:如果 URL 类似于上面的代码
https://storage.googleapis.com/bucketName/objectName
,浏览器应该可以直接查看它,只要满足几个条件:
application/octet-stream
, and web browsers will probably decide to just download such an object rather than displaying it in some form.application/octet-stream
,Web 浏览器可能会决定只下载这样的对象而不是以某种形式显示它。attachment; filename=foo.txt
attachment; filename=foo.txt
东西,可以强制将对象作为附件下载attachment; filename=foo.txt
attachment; filename=foo.txt
. attachment; filename=foo.txt
。allUsers
read permission.allUsers
读取权限。 Alternately, you could set the default object ACL property of the bucket to include that permission. In your case, the object is downloading successfully, so it's not the ACL issue, and if you don't know about the contentDisposition setting, then it's probably problem #1.在你的情况下,对象下载成功,所以这不是 ACL 问题,如果你不知道 contentDisposition 设置,那么它可能是问题 #1。 Make sure you specify a reasonable content type for the object.
确保为对象指定合理的内容类型。
Example:例子:
const blobStream = blob.createWriteStream({
resumable: false
contentType: "text/html"
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.