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
Access to the Google Cloud console If you are: A user granted access to a project. Use: https://console.cloud.google.com/ . A current project owner can give you access to the entire project, which applies equally to all buckets and objects defined in the project.
You can use Google Cloud Storage to store data in Google's cloud. Cloud Storage is typically used to store unstructured data. You can add objects of any kind and size, and up to 5 TB. Find Google Cloud Storage in the left side menu of the Google Cloud Platform Console, under Storage.
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"
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With