Reputation: 365
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]
When the upload is done, I get the URL and when I open it , the file is downloaded directly (image, pdf etc...)
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
Upvotes: 9
Views: 6444
Reputation: 38389
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:
application/octet-stream
, and web browsers will probably decide to just download such an object rather than displaying it in some form.attachment; filename=foo.txt
.allUsers
read permission. 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. Make sure you specify a reasonable content type for the object.
Example:
const blobStream = blob.createWriteStream({
resumable: false
contentType: "text/html"
})
Upvotes: 10