Unable to upload video via GraphQL API

AlexWebLab
Shopify Partner
1 0 2
Hello,
 
I'm having an issue uploading a VIDEO via GraphQL API. No problems with IMAGEs.
 
What I'm trying to achieve here is to copy some media from a product of one store to a product of another store.
 
Images are okay and actually, I don't even need to use the "stagedUploadsCreate" mutation. Still, I can use the "productCreateMedia" directly if I use an URL from Shopify CDN on "originalSource" field. Is it a bug? Let me know about this too.
 
The real problem is with the VIDEO media type. The video is already hosted in Shopify CDN since it belongs to a product already published.
 
First I tried using "productCreateMedia" directly like I'm doing with IMAGE type but I got an "Invalid video URL" error.

Okay then, I used the "stagedUploadsCreate" mutation. Here is the input object:
{
    filename: "ZZZZZZ.mp4"
    mimeType: "video/mp4"
    fileSize: "YYYYYY"
    resource: VIDEO
}
and got the following "resourceUrl": https:\/\/shopify-video-production-core-originals.storage.googleapis.com?external_video_id=XXXXXX
 
Then I tried the "productCreateMedia" with the following parameters:
productId: "gid://shopify/Product/XYXYXY"
media: [
{
    originalSource: "https:\/\/shopify-video-production-core-originals.storage.googleapis.com?external_video_id=XXXXXX"
    mediaContentType: VIDEO
}
]
And I got the UPLOADED status with the following ID: "gid:\/\/shopify\/Video\/XZXZXZ".
 
Then when I retrieved the media, I got FAILED status.
 
I hope someone can help with this. Thanks.
UX / Front-end / Full-stack Web Developer & IT Manager
Replies 6 (6)

Umiko
Shopify Staff
42 8 13

Hi @AlexWebLab  👋

 

After the `stagedUploadsCreate` mutation, the video will have to be uploaded to the target. Keep in mind that for videos, this should be a POST request to the `stagedTarget.url` in a multipart form including all parameters from `stagedTarget.parameters` field in preserved order. 

 

Once the video has been successfully uploaded to the target, the `productCreateMedia` mutation can consume the `stagedTarget.resourceUrl`. I'd recommend reviewing this guide to manage media for products as well

 

Here is an implementation using pre/post-request scripts with the Postman client, and similar logic can be applied in your app's native language.

// post stagedUploadsCreate request
const r = pm.response.json(); //response from stagedUploadsCreate mutation
const target = r.data.stagedUploadsCreate.stagedTargets[0]

pm.collectionVariables.set("target-url", target.url);
pm.collectionVariables.set("target-params", target.parameters);
pm.collectionVariables.set(
    "target-resource-url",
    JSON.stringify(target.resourceUrl)
);

// pre video upload request
const params = Array.from(pm.collectionVariables.get("target-params"));
params.reverse().forEach( p => {
    pm.request.body.formdata.prepend({
        key: p.name, 
        value: p.value
    })
})

 

Hope that helps!

Umiko | API Support @ 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

AlexWebLab_
Visitor
1 0 0

Hello Umiko,

Thank you for your time.

 

Your suggestion helps and it works if I'm uploading the video from my computer.

 

What I'm trying to achieve here is to copy media from a product of one store to a product of another store.

 

In this guide: Manage media for products it's stated that IF the asset is hosted by Shopify, THEN I can skip the "stagedUploadsCreate" mutation. And actually, I can do that with the IMAGE media type but not with the VIDEO media type. That's the problem.

 

It's unclear if I can also migrate VIDEOS other than IMAGES without the need of downloading them first on my computer.

 

Thanks for your help!

Matthias7
Excursionist
30 3 3

Hey AlexWebLab_

 

I am facing a similar problem: receving "Invalid video url" error on trying to use the "ProductCreateMedia" GraphQL mutation with a shopify hosted video.

 

Using my current workflow I can upload an "EXTERNAL_VIDEO" video type, so my query and CURL functions are working.

I wondered if you found a solution to your problem, in the mean time Ill keep looking 😉

 

Thanks for your time and cheers,

 

Matthias

ShopifyDevSup
Shopify Staff
1435 234 488

Hi @Matthias 👋

 

When using the `productCreateMedia` mutation, the `CreateMediaInput.originalSource` field should be an external URL or a staged upload URL. This means that if you pass a Shopify CDN hosted URL, it will not be accepted an "external URL" and expects the staged upload URL instead. 
 

Since videos hosted on one Shopify store with a Shopify CDN URLs, are not meant to be used to create product media on other Shopify stores, it will likely need to be downloaded and restaged in the new store.

Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

Matthias7
Excursionist
30 3 3

Hi @ShopifyDevSup ,

Thank you very much for your reply. 


Ok, that would be the problem.

 

Cheers,

 

Matthias

abhiroop
Shopify Partner
2 0 1

Hi @ShopifyDevSup,
How can I reuse the same VIDEO from an existing product to create a new product in the "same" store? It doesn't make sense to download, restage and upload the video again since the VIDEO is on the same store right? Can we do this using the Files and Media API, so that the same media can be used in different products?