How do I shorten Google Cloud Storage Signed download URLs?

别等时光非礼了梦想. 提交于 2020-08-27 21:49:40

问题


I have a firebase app, and I use Firebase Storage to upload images. the URLs i get back when I use the firebase web sdk to upload are reasonable:

https://firebasestorage.googleapis.com/v0/b/projectId.appspot.com/o/image.jpg?alt=media&token=51183d4a-551a-41e2-b620-14b44b8c86ed

However, since Firebase doesn't support the storage API in their node.js SDK, I have to use the Google Cloud Storage SDK:

bucketRef.upload(localImagePath, options, (err, file, response) => {
                    file.getSignedUrl({
                        action: 'read',
                        expires: '03-17-2030'
                    }, (err, url) => {
                        console.log(url)
                    })
                })

This returns the prohibitively long:

https://storage.googleapis.com/projectId.appspot.com/image.jpg?Googl
eAccessId=firebase-adminsdk-xfe5z@projectId.iam.gserviceaccount.com&Expires=1899950400&Si
gnature=fyotCYAbiWGuBGjwL0YDpByqZsKTdrwd9%2F7bZ88Rw8zP53dyEvcrIer6paYdzb%2BlH7OmJSRfcSxaAj7ur
GhZw20a4k4b5InLufqrOhSuYAE6w5vM2Hp8vz1XgSXl9jOFym2wMPEn7RkVwjxnT3QJKSBa0vqnkXX0wQUF4CjvHjUxbS
Tc9jj0NeNYUNmHGlZlVcKf%2BgE00rG9gt3QyCGAMt55h3kltMbyT%2FvnAYh%2FwuvhbVhX%2FNSCjieYb13KjjmDTgt
l5NU5nWY9Cu0QBraAbn6GlsUUvj0hBB2Gi7OHnUNi218w3EPehy7YAy6RhTcnuhiZlFaLX3TSmBS%2BX%2F3%2BoA%3D%
3D

I keep these URLs in firebase, so this stands to be kind of a storage burden as the # of images I'm storing starts to approach the 6-7 digit range.

Is there a way to shorten this?


回答1:


The short answer is no, you can't shorten it. The long answer is that you don't want to shorten it: the signature is a cryptographically signed hash that encapsulates the information presented above (action and expires) and lets the end user perform that action so long as the URL is valid.

If it were shorter, someone could guess the URL for your data, or potentially break it by a known plaintext attack (hashing it in the same way with given known plaintexts and seeing what matches).

See the GCS Docs on Signed URLs for more info.




回答2:


FYI, if you have multiple URLs, you can compress them and save about half:

len(one_url) == 617
len(zlib.compress(one_url)) == 494

len(my_12_urls_object) == 7836
len(zlib.compress(my_12_urls_object)) == 3583

You would probably save a lot more if they were all signed at the same time, however, mine are all signed at different times, so the URLs have less in common.

My use case is I send objects that have a list of photo thumbnails to a search api that imposes a maximum json object size.



来源:https://stackoverflow.com/questions/40701105/how-do-i-shorten-google-cloud-storage-signed-download-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!