File Upload via iControl REST

Code :

def _upload(host, creds, fp):

    chunk_size = 512 * 1024
    headers = {
        'Content-Type': 'application/octet-stream'
    }
    fileobj = open(fp, 'rb')
    filename = os.path.basename(fp)
    if os.path.splitext(filename)[-1] == '.iso':
        uri = 'https://%s/mgmt/cm/autodeploy/software-image-uploads/%s' % (host, filename)
    else:
        uri = 'https://%s/mgmt/shared/file-transfer/uploads/%s' % (host, filename)

    requests.packages.urllib3.disable_warnings()
    size = os.path.getsize(fp)

    start = 0

    while True:
        file_slice = fileobj.read(chunk_size)
        if not file_slice:
            break

        current_bytes = len(file_slice)
        if current_bytes < chunk_size:
            end = size
        else:
            end = start + current_bytes

        content_range = "%s-%s/%s" % (start, end - 1, size)
        headers['Content-Range'] = content_range
        requests.post(uri,
                      auth=creds,
                      data=file_slice,
                      headers=headers,
                      verify=False)

        start += current_bytes

if __name__ == "__main__":
    import os, requests, argparse, getpass

    parser = argparse.ArgumentParser(description='Upload File to BIG-IP')

    parser.add_argument("host", help='BIG-IP IP or Hostname', )
    parser.add_argument("username", help='BIG-IP Username')
    parser.add_argument("filepath", help='Source Filename with Absolute Path')
    args = vars(parser.parse_args())

    hostname = args['host']
    username = args['username']
    filepath = args['filepath']

    print "%s, enter your password: " % args['username'],
    password = getpass.getpass()

    _upload(hostname, (username, password), filepath)

Tested this on version:

12.0
Published Nov 05, 2015
Version 1.0
  • I don't suppose anyone has a powershell example of this? I am trying to create something to upload and also overwrite ifiles programatically.

     

  • powershell is not my strong suit but I can probably hack one together for you. I'll be at F5 Agility this week though, so probably can't get to it until next Monday.

     

  • Does anybody can provide curl command? I'm trying this: curl -sk -u admin:'*****' -H "Content-Type: application/octet‐stream" -d @my_file.xml -X POST

     

    In response I'm receiving: {"code":400,"message":"Content-Range",...

     

    But file size is just 74kB

     

  • Just found answer to my question. I'm using 11.6 and it's working fine:

     

    curl -i -sk -u admin:'*****' -X POST -H "Expect:" -H "Content-Type: application/octet-stream" -H "Content-Range: 0-(SIZE-1)/SIZE" --data-binary "@my_file.xml"

     

    File is located in: /ts/var/rest

     

    Next step rewrite it to use LWP :)