I am trying to upload a binary file / remote file using api_send_call mutation. The flow is similar to cURL and file_get_content in PHP.
Here the code:
{%- liquid
assign response = "{}" | parse_json
hash_assign response['remote_file'] = context.params.file_url | split: "?" | first
hash_assign response['filename'] = response.remote_file | split: "/" | last
hash_assign response['filepath'] = "modules/homepage/" | append: response.filename
hash_assign response['filetype'] = 'image/png'
-%}
{%- graphql get_assets_presign_url, path: response.filepath -%}
mutation( $path: String! ) {
ooc: admin_assets_presign_urls(
paths: {
path: $path
}
) {
rs: urls {
access_url
path
upload_url
}
}
}
{%- endgraphql -%}
{%- hash_assign response['assets_payload'] = get_assets_presign_url | dig: "ooc", "rs", 0 -%}
{% background priority: 'high', source_name: "PUT Method Upload", data: response %}
{%- graphql PUTOBJECT, payload: data -%}
mutation ApiCallSend( $payload: HashObject ) {
ooc: api_call_send(
template: {
name: "modules/homepage/putobject"
}
data: $payload
) {
rs: response {
body
status
headers
}
}
}
{%- endgraphql -%}
{%- log "----------- BACKGROUND JOB: PUT Method Upload ---------------", env: "staging" -%}
{%- log PUTOBJECT, type: "Upload remote file", env: "staging" -%}
{%- endbackground -%}
And this is the api_call code:
---
to: '{{ form.assets_payload.upload_url }}'
format: http
delay: 0
request_type: PUT
request_headers: '{
"Content-Type": "{{ form.filetype }}"
}'
---
{{- form.remote_file | download_file | html_safe -}}
I'm using PUT method.
But, I always get an "Access Denied" error message when I use:
to: '{{ form.assets_payload.upload_url }}'
However the upload is successful, if I use the Presigned URL directly like this:
to: 'https://HOST®ION.amazonaws.com/uploads.staging.oregon.platform-os.com/instances/INSTANCE NUMBER/assets/modules/homepage/cow.png?x-amz-acl=public-read&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=CREDENTIAL%2F20210825%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20210825T230934Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=SIGNATURE3aaa3f6b8211b40ad8feba963c987c1'
Even though it worked, I got a Graphql Error message: "string contains null bytes url"...
And I found another way to upload a binary file, from this page: https://documentation.platformos.com/developer-guide/apis/sending-binary-file-using-api-call-notification, but I don't know how to make it work with amazon S3.
Thanks.