# API upload via url inconsistent results

**URL:** https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016
**Category:** Development
**Tags:** rest-api
**Created:** [March 28, 2018, 4:46am UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016 "2018-03-28T04:46:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![michael.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michael.smith/32/93123_2.png) [@michael.smith](https://meta.discourse.org/u/michael.smith)
#### Post date: [March 28, 2018, 4:46am UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/1 "2018-03-28T04:46:16Z")

</div>

Hi All,

Looking for some insight into this one.  
I am trying to get uploads to work via API and I keep getting the error

```
{"errors"=>["Sorry, you must provide a file to upload."]}

```

This happens when I try to upload a generated public url from canvas i.e.

```
https://instructure-uploads-apse2.s3.ap-southeast-2.amazonaws.com/account_24310000000000001/attachments/777/IT%20Helpdesk%20Support%20Duty%20Statement%205%20Nov%202015.pdf?response-content-disposition=inline%3B%20filename%3D%22IT%20Helpdesk%20Support%20Duty%20Statement%205%20Nov%202015.pdf%22%3B%20filename%2A%3DUTF-8%27%27IT%2520Helpdesk%2520Support%2520Duty%2520Statement%25205%2520Nov%25202015.pdf&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIZAVS3WMRR54DT4Q%2F20180328%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20180328T032334Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=d4407ec41af0ffbf0bff523dba1322a18b726939242383876ed92ab1798977ee

```

If I use another url i.e.

```
http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf

```

it seems to work.

I can definitely browse to the url via browser and display the pdf file.  
What am I doing wrong here?

Thanks  
Michael

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [March 28, 2018, 3:22pm UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/2 "2018-03-28T15:22:32Z")

</div>

It looks like that url isn’t browsable and has some permission locking on it. Your browser can access it because you are logged in to aws, but your Discourse instance cannot access it because it has not been authorized.

---

<div class="post-metadata">

### Author: ![michael.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michael.smith/32/93123_2.png) [@michael.smith](https://meta.discourse.org/u/michael.smith)
#### Post date: [March 28, 2018, 8:15pm UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/3 "2018-03-28T20:15:37Z")

</div>

The URL has an expiry set on it, that is probably why you can’t see it now. It is generated on the fly when using the [Canvas API](https://canvas.instructure.com/doc/api/files.html#method.files.public_url)

It is difficult to show you but here is another URL that has just been generated. I can access it from incognito browser too.

`https://instructure-uploads-apse2.s3.ap-southeast-2.amazonaws.com/account_24310000000000001/attachments/777/IT%20Helpdesk%20Support%20Duty%20Statement%205%20Nov%202015.pdf?response-content-disposition=inline%3B%20filename%3D%22IT%20Helpdesk%20Support%20Duty%20Statement%205%20Nov%202015.pdf%22%3B%20filename%2A%3DUTF-8%27%27IT%2520Helpdesk%2520Support%2520Duty%2520Statement%25205%2520Nov%25202015.pdf&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIZAVS3WMRR54DT4Q%2F20180328%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20180328T201440Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=a476c1046e6b356c3bee618247957aea3c1dce3f0f442391130515a030fb8016`

I guess I am just trying to find out why these kinds of files are not able to be uploaded via Discourse API and others are.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [March 28, 2018, 8:31pm UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/4 "2018-03-28T20:31:26Z")

</div>

Something is still up with that url. Chrome is just being fancy and loading it right away for you (handling the headers or something).

 ![image](https://global.discourse-cdn.com/meta/original/3X/7/2/72403a4ea7d674b2c176d169558f30df00541ef9.png)

Once you are able to get it to work with `curl -O <url>` it should work in Discourse.

---

<div class="post-metadata">

### Author: ![michael.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michael.smith/32/93123_2.png) [@michael.smith](https://meta.discourse.org/u/michael.smith)
#### Post date: [March 29, 2018, 4:01am UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/5 "2018-03-29T04:01:19Z")

</div>

When using `curl -O <url>` It kept giving errors: `Warning: Failed to create the file`  
But I can get `curl -0 <url> -o filename.pdf` to work.  
Can discourse use other curl command lines? Or can I somehow get it to perform the same download as `curl -0 <url> -o filename`

Thanks

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [March 29, 2018, 3:30pm UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/6 "2018-03-29T15:30:28Z")

</div>

> [@michael.smith](#):
>
> But I can get curl -0 \<url\> -o filename.pdf to work.

I’m unable to get `curl -0 <url> -o filename.pdf` to work. Can you verify that the filename.pdf is the actual pdf document and not just some xml error.

Also `-0` just changes it to “Use HTTP 1.0 (H)” where as `-O` saves it as a file instead of trying to render the pdf in the console.

The Discourse app doesn’t actually use curl to download the file, but it is just a good way to test anonymous downloads of files and when the discourse app uses ruby to download the file it does do something similar to curl.

In order to determine what the issue is here can you provide some more info on how your s3 bucket file permissions are setup? How do you generate all the extra url parameters to allow “anonymous” access to a private file?

Another option might be to use curl or whatever your using to access the discourse api to download the file or use the aws-sdk to download the file as an authenticated user and then upload the actual file to the discourse api instead of just a url.

Also once you get this all working you might want to turn on this setting in Discourse “prevent anons from downloading files”.

---

<div class="post-metadata">

### Author: ![michael.smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michael.smith/32/93123_2.png) [@michael.smith](https://meta.discourse.org/u/michael.smith)
#### Post date: [April 3, 2018, 11:49pm UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/7 "2018-04-03T23:49:45Z")

</div>

Thanks for the tip.  
I ended up downloading the file to local storage and then uploading to Discourse.  
A bit hacky but works well.

Thanks

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [August 7, 2020, 6:19pm UTC](https://meta.discourse.org/t/api-upload-via-url-inconsistent-results/84016/8 "2020-08-07T18:19:33Z")

</div>


