Uploading File from REST calls
Hi,
I am having trouble uploading a file as a bytestream to Box via python REST calls.
I am using the cURL command found on the developer pages for reference, but am unsure how to handle the attributes field.
Also, on the documentation about uploading, it mentions that for best performance that the file bytes should come after the JSON. However, when I try to pass in the file attributes (name and parent) as JSON along with the bytestream itself, I receive a 405 error for method not allowed.
Could someone please provide some guidance or an example of how to upload a file using REST calls from Python?
Thank you very much!
-
Hi there,
I am not sure if you are using Box Python SDK for this or not. Here is a link for the SDK: Box Python SDK
Basically, when using SDK, you would do the following to upload a file. Obviously you would need to be authenticated first with proper tokens.
from StringIO import StringIO stream = StringIO() stream.write('Box Python SDK test!') stream.seek(0) box_file = client.folder('0').upload_stream(stream, 'box-python-sdk-test.txt') print box_file.name
here, the parent is '0' and the name of the file is 'box-python-sdk-test.txt'. It returns the file object in json. It prints the filename as an output in this example.
I would recommended to use SDK. It will prevent you from handling all the network backend work, queuing and much of the overhead.
hope this helps.
thanks,
Bibek
-
Hi ,
Thank you for your response. Unfortuately, I'm trying to do this without the python SDK but rather with the python Requests.request library. Do you have any idea how I'd go about doing that? Like I mentioned, it's the "attributes" that seem to be causing the problem because Box appears not to accept JSON as a valid method.
-
Hi ,
You can try something like this if you are using requests module to pass attribute.
data = {'attributes': json.dumps({
'name': file_name,
'parent': {'id': parent_folder_id},
})}files = {
'file': ('unused', file_stream),
}
box_response = self._session.post(url, data=data, files=files, expect_json_response=False)file_response = box_response.json()['entries'][0]
file_id = file_response['id']where file_stream is the object containing the bytes. You need to modify the request according to your methods anyway. But the main thing is you need to pass "data" into the POST request like the above sample.
hope this helps.
thanks,
Bibek
-
In the example here we upload a text file (.txt). Is is possible to upload as a boxnote (.boxnote) i.e. box-python-sdk-test.boxnote.
When I do this, the code doesn't complain however when I try an open the file in my browser I get the following error:
Sorry, there's been a problem with the contents of this note.
We have created a recovered note in the same folder (the file name ends with "(RECOVERED)").
-
I am trying to upload file with'rest-client' gems. I have tried but no luck. Here the code I tried so far.
content_type = MIME::Types.type_for('/home/vagrant/mukesh.txt').first.content_type headers = { "Authorization"=> "Bearer ", "Content-Type"=> content_type } payload = { "attributes"=> {"name"=>"mukesh.txt", "parent"=>{"id"=>"0"}}, "file"=> File.new('/home/vagrant/mukesh.txt', 'rb'), "multipart"=> true } response = RestClient::Request.execute(:method=> :post,:url=>"https://upload.box.com/api/2.0/files/content",:payload=>payload , :headers=> headers)
This is giving me 405 Method Not Allowed errors.
サインインしてコメントを残してください。
コメント
6件のコメント