StagedUploads get a permanent CDN url

Solved

StagedUploads get a permanent CDN url

kelreel
Shopify Partner
2 0 0
Hi! I use the stagedUploadsCreate method to upload the file, then use the fileCreate method throw GraphQL API. But the url received in the "resourceUrl" field is not quite correct - the file is deleted from the staged cdn (shopify-staged-uploads.storage . googleapis ...) after a few days. How do I get the final url of a file that is served via Shopify CDN (as in "settings" - "files" in Admin panel)?
 
image.png

Accepted Solution (1)
ShopifyDevSup
Shopify Staff
1453 238 508

This is an accepted solution.

The returned `file` is an interface, so you will need to fragment on the `GenericFile`,`MediaImage` a Video objects depending on the type of file you've created. It can be fetched as part of the fileCreate mutation or by querying the files to fragment:

mutation fileCreate($files: [FileCreateInput!]!) {
    fileCreate(files: $files) {
        files {
            ... Files
        }
    }
}

query {
    files (first:3, sortKey: CREATED_AT, reverse: true){
        nodes {
            ... Files
        }
    }
}

fragment Files on File {
    ... on GenericFile {
        url
    }
    ... on MediaImage {
        image {
            url
        }
    }
    ... on Video {
        sources {
            url
        }
    }
}

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

View solution in original post

Replies 5 (5)

ShopifyDevSup
Shopify Staff
1453 238 508

Hi @kelreel 👋

 

You can use the `fileCreate` mutation to create file assets that are added to the Admin > Settings > Files page.

 

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

kelreel
Shopify Partner
2 0 0

I am already using fileCreate mutation, how can I programmatically get the url of the file from it, which will be saved on the Shopify CDN?

ShopifyDevSup
Shopify Staff
1453 238 508

This is an accepted solution.

The returned `file` is an interface, so you will need to fragment on the `GenericFile`,`MediaImage` a Video objects depending on the type of file you've created. It can be fetched as part of the fileCreate mutation or by querying the files to fragment:

mutation fileCreate($files: [FileCreateInput!]!) {
    fileCreate(files: $files) {
        files {
            ... Files
        }
    }
}

query {
    files (first:3, sortKey: CREATED_AT, reverse: true){
        nodes {
            ... Files
        }
    }
}

fragment Files on File {
    ... on GenericFile {
        url
    }
    ... on MediaImage {
        image {
            url
        }
    }
    ... on Video {
        sources {
            url
        }
    }
}

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

jam_chan
Shopify Partner
917 23 187

@ShopifyDevSup 

 

I try your code. It looks ok but the query is giving a null for the latest image:

{
    'data': {
        'files': {
            'nodes': [{
                    'createdAt': '2023-03-08T04:46:26Z',
                    'image': None
                }, {
                    'createdAt': '2023-03-07T10:32:59Z',
                    'image': {
                        'url': 'https://cdn.shopify.com/s/files/1/1498/3202/files/vadim-sherbakov-tCICLJ5ktBE-unsplash.jpg?v=1678185180'
                    }
                }, {
                    'createdAt': '2023-03-07T10:11:43Z',
                    'image': {
                        'url': 'https://cdn.shopify.com/s/files/1/1498/3202/files/food-og4.jpg?v=1678183904'
                    }
                }
            ]
        }
    },
    'extensions': {
        'cost': {
            'requestedQueryCost': 8,
            'actualQueryCost': 6,
            'throttleStatus': {
                'maximumAvailable': 1000.0,
                'currentlyAvailable': 994,
                'restoreRate': 50.0
            }
        }
    }
}

 After the file is created, the image url cannot be fetched immediately. I try to use Shopify GraphiQL App to run query again. The image url is shown again. What can I do here? Try to poll for the latest image?

BYOB - Build Your Own Bundles, SPO - SEO App to research keywords & edit social link preview
ShopifyDevSup
Shopify Staff
1453 238 508

Hi @jam_chan 👋

 

I'd recommend using `MediaImage.status` and `MediaImage.fileStatus` to poll and determine whether the URL has been generated yet.

 

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