Get URL in Callback Response with Simple Upload Form
Hi,
I'm working on a simple HTML upload image form, setting api_key, callback, signature, and other values in hidden fields, and it uploads fine. (I'm trying to avoid client scripting, since some of my clients have older browsers). My backend is PHP, I'm signing the uploads, and the plan is to have users upload to Cloudinary, and then fetch + record the file specifics when they are returned in a GET callback/redirect.
When the callback redirect occurs, however, the data coming back in the GET parameters do not include the full URL(s) of the uploaded files. Example output from PHP _GET:
.
array(16) { ["access_mode"]=> string(6) "public" ["asset_id"]=> string(32) "2343cb6389513c175007086ecb66b07d" ["bytes"]=> string(3) "176" ["created_at"]=> string(20) "2022-03-23T20:52:11Z" ["etag"]=> string(32) "3a5ae6a3063418d2c923ed2a28e5984f" ["format"]=> string(3) "png" ["height"]=> string(2) "24" ["overwritten"]=> string(4) "true" ["placeholder"]=> string(5) "false" ["public_id"]=> string(3) "foo" ["resource_type"]=> string(5) "image" ["signature"]=> string(40) "cdbd78e6eb282b50ac904e367a6e5a75b46269ce" ["type"]=> string(6) "upload" ["version"]=> string(10) "1648077720" ["version_id"]=> string(32) "0ccd602896a5d56d0dd2c9b1ec50bf53" ["width"]=> string(2) "24" }
.
Since the user uploaded directly to Cloudinary, I rely on getting back all of the file data so I can record it.
Without the URL, I don't have access to the image's full path that is shown in the media library for an image. For example, for an image uploaded to cloudinary.com/mycloudname/image/upload/v1648074883/foo.png, I don't have the "v1648074883" part, since it's never returned to me in the GET.
My question is: since I'm setting the public_id value, and the file's extension/type is returned to me, I can still access the image publicly by visiting cloudinary.com/mycloudname/image/upload/foofoo.png (that is, omitting the v1648074883 part of the URL, which I never receive).
Is this fine, expected behavior? Just want to make sure this method of referencing (going straight to /upload/filename.extension) isn't going to disappear in some future build.
Hitting an image at /upload/filename isn't ideal, because if a user uploads several times and they are all saved as filename foo, the CDN may not cache the latest one right away, so I end up with a variety of "foo" images showing up until the CDN finally catches up.
For future releases, returning the full URL as part of the callback GET payload during a basic web POST might be helpful, so we can reference the latest image and avoid caching issues.
P.S. -- I'm able to successfully retrieve the file details via an adminAPI call *after* the upload callback, but I read that Admin calls are limited per month, so probably best to avoid that route (uploading, then calling back for details).
Thank you!
-
Hi,
My question is: since I'm setting the public_id value, and the file's extension/type is returned to me, I can still access the image publicly by visiting cloudinary.com/mycloudname/image/upload/foofoo.png (that is, omitting the v1648074883 part of the URL, which I never receive).Is this fine, expected behavior? Just want to make sure this method of referencing (going straight to /upload/filename.extension) isn't going to disappear in some future build.
- The full version e.g. `v1648074883` can be omitted from the URL. It is used mainly for cache busting, meaning you want to pull the latest version of the image, you can just update that value to the current timestamp and it will fetch the latest asset.
Hitting an image at /upload/filename isn't ideal, because if a user uploads several times and they are all saved as filename foo, the CDN may not cache the latest one right away, so I end up with a variety of "foo" images showing up until the CDN finally catches up.
- If they all have the same public id, just make sure to include the parameters `invalidate` and `overwrite` set to true, and those assets will be invalidated at the CDN, so you should always get the latest. When you destroy an asset you should also do `invalidate` set to `true`.
or future releases, returning the full URL as part of the callback GET payload during a basic web POST might be helpful, so we can reference the latest image and avoid caching issues.
- This shouldn't matter, the upload response you shared has everything you need to generate the cloudinary URL. In your database you should save `public_id`, `format`, `resource_type`, `type` and if needed `version`. With these parameters you can generate the Cloudinary URL via one of our SDKs or just by constructing it yourself here.
Hope this helps.
Post is closed for comments.
Comments
1 comment