Uploading File from REST calls

新規投稿

コメント

6件のコメント

  • Bibek

    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 

     

     

    0
    コメントアクション パーマリンク
  • Box Product Support

    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.

     

     

    0
    コメントアクション パーマリンク
  • Bibek

    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

     

    0
    コメントアクション パーマリンク
  • mscarr

    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)").

    0
    コメントアクション パーマリンク
  • m900142

    I'm encountering this same 405 Method Not Allowed error.  Asambors, did you get this to work and if so, what did you need to do? 

    0
    コメントアクション パーマリンク
  • novice_mux

    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.

     

    0
    コメントアクション パーマリンク

サインインしてコメントを残してください。