-
Notifications
You must be signed in to change notification settings - Fork 4
/
s3cp.py
executable file
·51 lines (39 loc) · 1.61 KB
/
s3cp.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
#!/usr/bin/env python
# -*- coding: latin-1 -*-
import requests, subprocess, shlex, urlparse, os, sys
AUTHID='user'; AUTHPW='secret'; HEADERS = {'content-type': 'application/json'}; SERVER = 'https://www.encodeproject.org/'
S3_SERVER='s3://encode-files/'
#get all the file objects
files = requests.get(
'https://www.encodeproject.org/search/?type=file&frame=embedded&limit=all',
auth=(AUTHID,AUTHPW), headers=HEADERS).json()['@graph']
#select your file
f_obj = files[123]
#make the URL that will get redirected - get it from the file object's href property
encode_url = urlparse.urljoin(SERVER,f_obj.get('href'))
#stream=True avoids actually downloading the file, but it evaluates the redirection
r = requests.get(encode_url, auth=(AUTHID,AUTHPW), headers=HEADERS, allow_redirects=True, stream=True)
try:
r.raise_for_status
except:
print '%s href does not resolve' %(f_obj.get('accession'))
sys.exit()
#this is the actual S3 https URL after redirection
s3_url = r.url
#release the connection
r.close()
#split up the url into components
o = urlparse.urlparse(s3_url)
#pull out the filename
filename = os.path.basename(o.path)
#hack together the s3 cp url (with the s3 method instead of https)
bucket_url = S3_SERVER.rstrip('/') + o.path
#print bucket_url
#ls the file from the bucket
s3ls_string = subprocess.check_output(shlex.split('aws s3 ls %s' %(bucket_url)))
if s3ls_string.rstrip() == "":
print >> sys.stderr, "%s not in bucket" %(bucket_url)
else:
print "%s %s" %(f_obj.get('accession'), s3ls_string.rstrip())
#do the actual s3 cp
#return_value = subprocess.check_call(shlex.split('aws s3 cp %s %s' %(bucket_url, filename)))