如何获取使用fileCreate上传的文件的URL?

huangapple go评论130阅读模式
英文:

How to get the url of a file uploaded using fileCreate?

问题

已按照文档中所示的示例进行了mutator的跟随。

这只返回文件上传时间(createdAt time)。如何获取图像的CDN URL?

英文:

Have followed the example mutator as shown in the documentation

  1. $query = <<<QUERY
  2. mutation fileCreate($files: [FileCreateInput!]!) {
  3. fileCreate(files: $files) {
  4. files {
  5. alt
  6. createdAt
  7. }
  8. userErrors {
  9. field
  10. message
  11. }
  12. }
  13. }
  14. QUERY;

This is just returning the createdAt time that the file was uploaded. How do I get the cdn url of the image?

答案1

得分: 1

这需要一个两步的方法。首先是修改原始突变以检索从Shopify生成的文件ID:

  1. $query = <<<QUERY
  2. mutation fileCreate($files: [FileCreateInput!]!) {
  3. fileCreate(files: $files) {
  4. files {
  5. alt
  6. createdAt
  7. ... on GenericFile {
  8. id
  9. }
  10. ... on MediaImage {
  11. id
  12. }
  13. ... on Video {
  14. id
  15. }
  16. }
  17. userErrors {
  18. field
  19. message
  20. }
  21. }
  22. }
  23. QUERY;

这将返回一个类似于gid://shopify/MediaImage/123456789012345的ID。

文件上传后,我们需要使用我们的ID调用一个files查询:

  1. $getFileQuery = <<<QUERY
  2. query {
  3. files(first: 1, query: &quot;id:{{ id }}&quot;) {
  4. edges {
  5. node {
  6. fileStatus
  7. ... on MediaImage {
  8. image {
  9. url
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. QUERY;

当您上传文件时,Shopify 必须对其进行处理,因此状态为 "PROCESSING",直到处理完成,您才能获得 URL。因此,我将查询函数包装在一个 do/while 循环中:

  1. //Ping shopify for the image. Make sure it has finished being processed.
  2. $process = false;
  3. do {
  4. //Find my file by id
  5. $response = $api-&gt;query([&quot;query&quot; =&gt; str_replace(&#39;{{ id }}&#39;, str_replace(&#39;gid://shopify/MediaImage/&#39;, &#39;&#39;, $data[&#39;data&#39;][&#39;fileCreate&#39;][&#39;files&#39;][0][&#39;id&#39;]), $getFileQuery)]);
  6. $data = $response-&gt;getDecodedBody();
  7. //File still processing. Wait a moment and try again.
  8. if (
  9. !array_key_exists(0, $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;]) ||
  10. $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;fileStatus&#39;] != &#39;READY&#39;
  11. ) {
  12. sleep(1);
  13. continue;
  14. }
  15. //Found our file. Extract the image url
  16. $newFileName = $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;image&#39;][&#39;url&#39;];
  17. $process = true; //Break the loop.
  18. } while (!$process);
  19. //Use $newFileName to take over the world.
英文:

This requires a two-step approach. The first is to amend the original mutation to retrieve the generated file id from shopify:

  1. $query = &lt;&lt;&lt;QUERY
  2. mutation fileCreate($files: [FileCreateInput!]!) {
  3. fileCreate(files: $files) {
  4. files {
  5. alt
  6. createdAt
  7. ... on GenericFile {
  8. id
  9. }
  10. ... on MediaImage {
  11. id
  12. }
  13. ... on Video {
  14. id
  15. }
  16. }
  17. userErrors {
  18. field
  19. message
  20. }
  21. }
  22. }
  23. QUERY;

This will return an id similar to gid://shopify/MediaImage/123456789012345

Once the file has been uploaded we need to call a files query with our ID:

  1. $getFileQuery = &lt;&lt;&lt;QUERY
  2. query {
  3. files(first: 1, query: &quot;id:{{ id }}&quot;) {
  4. edges {
  5. node {
  6. fileStatus
  7. ... on MediaImage {
  8. image {
  9. url
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }
  16. QUERY;

When you upload a file Shopify has to process it so the status is "PROCESSING" and you won't get the url out until it's finished. So I wrap the query function in a do/while loop:

  1. //Ping shopify for the image. Make sure it has finished being processed.
  2. $process = false;
  3. do {
  4. //Find my file by id
  5. $response = $api-&gt;query([&quot;query&quot; =&gt; str_replace(&#39;{{ id }}&#39;, str_replace(&#39;gid://shopify/MediaImage/&#39;, &#39;&#39;, $data[&#39;data&#39;][&#39;fileCreate&#39;][&#39;files&#39;][0][&#39;id&#39;]), $getFileQuery)]);
  6. $data = $response-&gt;getDecodedBody();
  7. //File still processing. Wait a moment and try again.
  8. if (
  9. !array_key_exists(0, $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;]) ||
  10. $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;fileStatus&#39;] != &#39;READY&#39;
  11. ) {
  12. sleep(1);
  13. continue;
  14. }
  15. //Found our file. Extract the image url
  16. $newFileName = $data[&#39;data&#39;][&#39;files&#39;][&#39;edges&#39;][0][&#39;node&#39;][&#39;image&#39;][&#39;url&#39;];
  17. $process = true; //Break the loop.
  18. } while (!$process);
  19. //Use $newFileName to take over the world.

Once Shopify has finished processing the file it sets the status as "READY" so you can grab the file url.

huangapple
  • 本文由 发表于 2023年6月15日 19:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定