Created
September 18, 2019 01:42
-
-
Save miniway/c2d442681434825fd7f0c974f80cc391 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import requests | |
apikey=sys.argv[1] | |
host=sys.argv[2] | |
file=sys.argv[3] | |
headers={ | |
"Authorization": "TD1 %s" % apikey, | |
"Content-Type": "application/json" | |
} | |
HOSTS={ | |
"dev-eu" : "https://api-plazma-development.eu01.treasuredata.com", | |
"stg-eu" : "https://api-plazma-staging.eu01.treasuredata.com", | |
} | |
def _url(host, path): | |
global HOsTS | |
host = HOSTS.get(host, host) | |
return '%s/api/%s' % (host, path) | |
def _send(resp): | |
print ('%s %s' % (resp.request.method, resp.url)) | |
if resp.status_code not in [200, 201]: | |
# This means something went wrong. | |
raise Exception('{} {} {}'.format(resp.url, resp.status_code, resp.text)) | |
return resp.json() | |
# start transaction | |
data = _send(requests.post(_url(host, 'transactions'), headers=headers)) | |
txn=data['uniqueName'] | |
# prepare upload | |
data = _send(requests.post(_url(host, 'transactions/%s/partitions/prepare' % txn), headers=headers)) | |
partition=data['partitionName'] | |
upload_headers = {"Content-Type": "application/octet-stream"} | |
if data["encrypt"]: | |
upload_headers["x-amz-server-side-encryption"] = "AES256" | |
# upload | |
with open(file, 'rb') as f: | |
print ("%s %s" % (data["uploadUrl"], upload_headers)) | |
resp = requests.put(data["uploadUrl"], data=f, headers=upload_headers) | |
if resp.status_code not in [200, 201]: | |
# This means something went wrong. | |
raise Exception('{} {} {}'.format(resp.url, resp.status_code, resp.text)) | |
print resp.text | |
# finish upload | |
body = {"databaseName": "testdb", "tableName": "testtable", "fileSize": os.path.getsize(file), "firstIndexKey" : 0, "lastIndexKey" : 1000, "recordCount": 1000} | |
print body | |
data = _send(requests.put(_url(host, 'transactions/%s/partitions/%s/finish' % (txn, partition)), json=body, headers=headers)) | |
# finish transaction | |
data = _send(requests.put(_url(host, 'transactions/%s/commit' % (txn)), headers=headers)) | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment