-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload_command.py
93 lines (76 loc) · 3.13 KB
/
upload_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Message describing how to use the script.
USAGE = """
Uploads the result of a command to storage
Note : Uploaded as text/plain format
Usage examples:
$ python upload_command.py "ls -la" gs://bucket/object
$ python
"""
# You need to create a service account key and download
# the key in json format.
# see here http://bit.ly/1tprtJc
# Path is relative
PRIVATE_KEY_PATH = '/path/to/key/xxxx.json'
import httplib2 # handle http stuff
import os # handle file
import sys # handle commmand line args
import io # for plain text upload (nkcr)
import json # to display the result (nkcr)
import subprocess # to make pg_dump or whatever unix command
from apiclient.discovery import build as discovery_build
from oauth2client.service_account import ServiceAccountCredentials
# for key auth (nkcr)
from apiclient.http import MediaIoBaseUpload
RW_SCOPE = 'https://www.googleapis.com/auth/devstorage.read_write'
def get_authenticated_service(scope):
print 'Authenticating...'
scopes = ["https://www.googleapis.com/auth/devstorage.read_write"]
credentials = ServiceAccountCredentials.from_json_keyfile_name(PRIVATE_KEY_PATH, scopes)
# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with the Credentials. Note that the first parameter, service_account_name,
# is the Email address created for the Service account. It must be the email
# address associated with the key that was created.
#credentials = SignedJwtAssertionCredentials(
# EMAIL_KEY,
# key,
# scope=scope)
http = httplib2.Http()
http = credentials.authorize(http)
return discovery_build('storage', 'v1', http=http)
def upload(argv):
command = argv[1]
bucket_name, object_name = argv[2][5:].split('/', 1)
assert bucket_name and object_name
service = get_authenticated_service(RW_SCOPE)
content = subprocess.check_output(command,shell = True)
print 'Building upload request...'
#media = MediaFileUpload(filename, chunksize=CHUNKSIZE, resumable=True)
media = MediaIoBaseUpload(io.BytesIO(content), 'text/plain;charset=utf-8')
request = service.objects().insert(bucket=bucket_name, name=object_name,
media_body=media)
print 'Uploading file: %s to bucket: %s object: %s ' % (command, bucket_name,
object_name)
resp = request.execute()
print json.dumps(resp, indent=2)
print '\nUpload complete!'
if __name__ == '__main__':
if len(sys.argv) < 3:
print 'Too few arguments.'
print USAGE
if sys.argv[2].startswith('gs://'):
upload(sys.argv)
else:
print USAGE