how do I upload a image to cloudinary via Unity c#. 502 Bad Gateway

Options
Ferrrari177
Ferrrari177 Member Posts: 5
edited July 2023 in Developer APIs

this is a test upload to get it working. The cloudName ,apikey, apiSecret are fake.

Error Code 502 Bad Gateway on calling UploadImage();


using System.Collections;

using UnityEngine;

using UnityEngine.Networking;


public class ImageUploader : MonoBehaviour

{

  public string cloudName = "ddfthjydfn"; // 

  public string apiKey = "34546345345353";

  public string apiSecret = "acQakere7d6gdfdfgFLJQPU_Te4y7QY";


  public string uploadPreset = "default"; // The upload preset defined in your Cloudinary account


  public string imagePath; // The local path of the image you want to upload


  public void UploadImage(Texture2D Textureimage)

  {

    StartCoroutine(UploadImageCoroutine(ConvertTextureToByteArray(Textureimage)));

  }


  private IEnumerator UploadImageCoroutine(byte[] imageBytes)

  {

    // Read the image file bytes

    //byte[] imageBytes = System.IO.File.ReadAllBytes(imagePath);


    // Construct the URL for the upload API endpoint


        string uploadUrl = $"https://api.cloudinary.com/v1_1/{cloudName}/image/upload";



    // Create a new UnityWebRequest instance


    UnityWebRequest request = new UnityWebRequest(uploadUrl, UnityWebRequest.kHttpVerbPOST);


    // Set the request method and headers

    request.method = UnityWebRequest.kHttpVerbPOST;

    request.SetRequestHeader("Content-Type", "multipart/form-data");


    // Set the form data parameters

    WWWForm form = new WWWForm();


    form.AddBinaryData("file", imageBytes, "image.png", "image/png");

    form.AddField("upload_preset", uploadPreset);

    form.AddField("api_key", apiKey);



    // Attach the form data to the request

    request.uploadHandler = new UploadHandlerRaw(form.data);

    request.downloadHandler = new DownloadHandlerBuffer();



    // Send the request

    yield return request.SendWebRequest();


    // Check for errors

    if (request.result == UnityWebRequest.Result.Success)

    {

      Debug.Log("Image uploaded successfully!");

    }

    else

    {

      Debug.LogError($"Error uploading image: {request.error}");

    }

  }



  public byte[] ConvertTextureToByteArray(Texture2D texture)

  {

    // Encode the texture to PNG format

    byte[] byteArray = texture.EncodeToPNG();


    return byteArray;

  }


}

Tagged:

Answers

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 53
    Options

    Hi @Ferrrari177,

    In order to investigate, could you share your cloud_name?

    Thanks in advance.

    Best,

    Loic

  • Ferrrari177
    Ferrrari177 Member Posts: 5
    Options

    Hi Loic, my cloud name is ddlakkdgn

    Thank you

    Lee

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 53
    Options

    Hi @Ferrrari177,

    I believe the 502 is coming from Unity.

    In our logs, I can see 1 upload on our side that is failing with an error 400 because you are setting the upload_preset to default which doesn't exist in your account.

    Can you try to set it to test_cld and see if that works? It is a preset I just created.

    Thanks,

    Loic

  • Ferrrari177
    Ferrrari177 Member Posts: 5
    Options

    Hi Loic,

    I changed the uploadPreset = "test_cld", but still have a 502 error.

    Thanks

    Lee

  • Ferrrari177
    Ferrrari177 Member Posts: 5
    Options

    Hi Loic,

    The apiSecret is no used on the upload, should this be?

    Lee

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 126 admin
    Options

    Hi Lee,

    Thank you for the update. Unsigned Upload is a mechanism in the Upload process which is controlled via upload preset. Please check the aforementioned link for detailed documentation.

    Regards,
    Anthony

    Developer Support Engineer
    Cloudinary


    Join the Cloudinary Community


  • Ferrrari177
    Ferrrari177 Member Posts: 5
    Options

    Thank you Anthony,

    I can see that the test_cld has been made for me by Loic and is unsigned. This still had the 502 error.

    I will spend a few days adjusting setting on the setting page and see If I can get it to upload.

    I will look though the documentation and try to work it out myself.

    Thanks

    Lee

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 126 admin
    Options

    Hi Lee,

    Thank you for the update. Please do not hesitate to reach out again if have any questions. I have similar integration but in Blender, in which I use the same unsigned upload mechanism to directly upload the 3D model to Cloudinary. Here's my repo: https://github.com/tdatu/cloudinary_exporter

    I hope that helps.

    Regards,
    Anthony

    Developer Support Engineer
    Cloudinary


    Join the Cloudinary Community