What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Upload image file using stagedUploadTargetGenerate

Solved

Upload image file using stagedUploadTargetGenerate

ykyuen
Shopify Partner
18 1 7

Hi all,

I am working the the Shopify admin API (API version: 2020-10) and i want to upload the product image using stagedUploadTargetGenerate. According to the documentation, i need to provide filename, mimeType and resource as input parameters and i used the following values:

 

 

const filename = '/u01/sony-vaio.jpg'; // Absolute path of the file to be uploaded
const mimeType = 'image/jpeg';
const resource = 'PRODUCT_IMAGE';

 

 

I made two requests and got the following response:

 

 

[
  {
    message: 'Internal error. Looks like something went wrong on our end.\n' +
      'Request ID: 3f5edabf-6b18-4ed8-a04b-5d17c5b47c3c (include this in support requests).',
    extensions: {
      code: 'INTERNAL_SERVER_ERROR',
      requestId: '3f5edabf-6b18-4ed8-a04b-5d17c5b47c3c'
    }
  }
]

[
  {
    message: 'Internal error. Looks like something went wrong on our end.\n' +
      'Request ID: 552f4e6d-bc19-4ebd-ac6f-6d395f292ad2 (include this in support requests).',
    extensions: {
      code: 'INTERNAL_SERVER_ERROR',
      requestId: '552f4e6d-bc19-4ebd-ac6f-6d395f292ad2'
    }
  }
]

 

 

I have contacted support and waiting for their repl. but in the mean time i want to check if there is anything i did wrongly. Here is part of my code for making the graphql request.

 

 

uploadImage: (auth, filename, mimeType, resource) => {
  const query = {
    query: `mutation stagedUploadTargetGenerate($input: StagedUploadTargetGenerateInput!) {
      stagedUploadTargetGenerate(input: $input) {
        userErrors {
          field
          message
        }
        url
        parameters {
          name
          value
        }
      }
    }`,
    variables : {
      input: {
        filename: filename,
        mimeType: mimeType,
        resource: resource
      }
    }
  };
  return shopifyGraphQLClient(query, auth);
}

 

 

Any ideas are welcome. Thanks.

Accepted Solution (1)

Kevin_A
Shopify Staff
318 42 61

This is an accepted solution.

Hey @ykyuen 

I took a quick look at our logs and the exception we are seeing is "InvalidPathError". You may want to try a relative path instead to see if that works. Also, take a look at this tutorial: https://shopify.dev/tutorials/manage-product-media-with-admin-api

Kevin_A | Solutions Engineer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 2 (2)

Kevin_A
Shopify Staff
318 42 61

This is an accepted solution.

Hey @ykyuen 

I took a quick look at our logs and the exception we are seeing is "InvalidPathError". You may want to try a relative path instead to see if that works. Also, take a look at this tutorial: https://shopify.dev/tutorials/manage-product-media-with-admin-api

Kevin_A | Solutions Engineer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

ykyuen
Shopify Partner
18 1 7

Hi Kevin,

Thanks for your reply. i finally get it working by using stagedUploadsCreate instead of stagedUploadTargetGenerate and previously i thought this request will upload the image so i try to specify the file path in the request parameters but it turns out the request only returns an AWS url for the real file upload by making another PUT request.

The article u posted https://shopify.dev/tutorials/manage-product-media-with-admin-api is very useful. Thanks again for your help.

Regards,
Kit