How to move one image from one folder to another?

rwahdan
rwahdan Member Posts: 51
edited April 12 in Developer APIs

I am trying to move an image from one folder to another with API and .NET

here is my code:

public string MoveImage(string oldPID, string newPID)

{           

var cloudinary = new Cloudinary(cloudinaryUrl: "cloudinary://334819583972299:?????");
RenameParams renameParams = new RenameParams(oldPID, newPID)           

{               

FromPublicId = oldPID,               

ToPublicId = newPID           

};
var renameResult = cloudinary.Rename(renameParams).JsonObj.ToString();

return renameResult;       

}

I got no errors in code but in postman I have set environemnt variables for

oldPID: "test/rami%20card.png.png",

newPID: "test2/rami%20card.png.png"

when I run postman I am getting:

error 404 Not Found!

I think this is because the public id includes / signs and it will think it is looking for a path not a value. How to solve this?

Tagged:

Best Answers

  • Stephen
    Stephen Member, Cloudinary Staff Posts: 30
    Answer ✓

    Hi Rami,

    To rename/move an asset, you should certainly use the "rename" API method from the Upload API :  https://community.cloudinary.com/home/leaving?allowTrusted=1&target=https%3A%2F%2Fcloudinary.com%2Fdocumentation%2Frename_assets

    If you have another ticket where the update API method was mentioned for changing the public_id, please let me know with a link and I'll check it for you and update the details there. The update method can be used in Dynamic Folders mode to change the asset_folder or display_name, but that does not apply to your account or to the public_id values.

    In our API call logs, I can see that you've attempted to call that API method recently, but with public_id values that don't match an existing asset, such as: eid.jpg, eid, eid.jpg.jpg

    I do see one named test/eid.jpgso if that's what you're trying to rename, your calls are missing the part of the public_id value that includes the folder path.

    Please check the values you're passing are being passed correctly from the response to an earlier API call and/or from your database after being confirmed as correct.

    Regarding those examples, please also note that for image and video assets the public_id should not include a file extension, as the 'format' property of the asset will be used to set the extension of the URL: https://cloudinary.com/documentation/upload_parameters#public_id

    If you're uploading assets and including a file extension in the public_id, you should change that. If the asset already has a file extension in the public_id, it should still be accessible and the exact public_id value should be used to access the asset via the API

    If it's still not working as you expect, it may be best to contact us directly via our support site ( https://support.cloudinary.com/ ) with the following information:

    • The exact API call you made, including all parameter values
    • Details of where those values came from (e.g. an earlier call to upload an asset)
    • The response received from our API server, either the error message or the result of a successful call
    • The approximate time of the API call so we can more easily find the corresponding logs.

    Regards,

    Stephen

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 139
    Answer ✓

    Hi @rwahdan.

    The extra extension is coming from the fact you've saved an extension in the public ID. We recommend using public IDs without file extensions, because a single asset can be delivered in many different formats, which can lead to confusion if the extension in part of the public ID. For instance, if you save an image as myimage.jpg and then choose to deliver it as a png, you would get https://res.cloudinary.com/yourcloudname/image/upload/myimage.jpg.png.

    What I would recommend is writing a short script to list all the assets in your account, and if there are any that contain a file extension, using the rename method to remove the extension.

    I hope this helps. Please let me know if you have any questions.

    All the best,

    -Danny

Answers

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 139

    Hi @rwahdan.

    From the error response it seems that a literal "FromPublicId" is being passed to the upload method, rather than the value of FromPublicId. Would you mind sharing your upload code here please?

    If you would prefer, you can raise a ticket via support.cloudinary.com along with your cloud name, and we'll be able to discuss this more privately.

    All the best,

    -Danny

  • rwahdan
    rwahdan Member Posts: 51

    public string MoveImage(string publicId)       

    {           

    var cloudinary =  new Cloudinary(cloudinaryUrl: "cloudinary://…");                       

    UpdateParams updateParams = new UpdateParams(publicId)           

    {

    PublicId = "test2/ramicard.png.png"           

    };

    cloudinary.UpdateResource(updateParams);           

    return updateParams.ToString();       

    }

  • rwahdan
    rwahdan Member Posts: 51

    here is what I have with no errors but the public id is not changed either, why?

    the parameter passed have the original public id : test/ramicard.png.png and the new one is test2/ramicard.png.png but it is not changing the location when browsing!

  • Stephen
    Stephen Member, Cloudinary Staff Posts: 30

    Hi Rami,

    I've checked the logs on our side and the only calls we're seeing to the update API for your account are failing with a 404 error because the call didn't match an existing asset.

    Your updated example screenshot is also calling the "update" method of the Admin API, which cannot rename an existing asset because the public_id parameter is to identify the asset that you want to update, and there isn't a different parameter to specify a new public_id for the identified asset.

    To rename/move an asset, you should use the "rename" API method from the Upload AIP - may I ask you to check that please?: https://cloudinary.com/documentation/rename_assets

    Though it wasn't the problem in this example, please also check that you're passing the correct resource type and {delivery} type properties when renaming.

    Those properties are optional but if they're not specified we assume it's an "image" of type "upload", so if you're renaming videos or raw files, or the delivery type is not "upload", the call will not work as you expect.

    Regards,

    Stephen

  • rwahdan
    rwahdan Member Posts: 51

    I asked an earlier question and they tolde me to use update since i can edit the public id in the site, now you tell me to do the rename which is not working for me as well. What should I do? You can check my earlier post which I have code that tells about the rename that I used with error!

  • rwahdan
    rwahdan Member Posts: 51

    So I have this code that don't have any errors but the image public id is not changed either, why?

    public string MoveImage(string publicId)

    {           

    var cloudinary = new Cloudinary(cloudinaryUrl: "cloudinary://???");                       

    UpdateParams updateParams = new UpdateParams(publicId)           

    {               

    PublicId = "test2/ramicard.png.png"           

    };
    cloudinary.UpdateResource(updateParams); 

    return updateParams.ToString();       

    }

  • rwahdan
    rwahdan Member Posts: 51

    OK i have it working but I am having a problem with imageurl! if i go to your site and copy url i can see the image name ramicard.png in the public id but in the url it is ramicard.png.png, from where i get that extra .png? I need to remove that, how?