Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata methods #118

Merged
merged 5 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions gcsfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,13 @@ def _call(self, method, path, *args, **kwargs):
validate_response(r, path)
break
except (HtmlError, RequestException, GoogleAuthError) as e:
logger.exception("_call exception: %s", e)
if retry == self.retries - 1:
logger.exception("_call out of retries on exception: %s", e)
raise e
if is_retriable(e):
# retry
logger.debug("_call retrying after exception: %s", e)
continue
logger.exception("_call non-retriable exception: %s", e)
raise e
try:
out = r.json()
Expand Down Expand Up @@ -918,7 +919,8 @@ def get(self, rpath, lpath, blocksize=5 * 2 ** 20):
f2.write(d)

@_tracemethod
def put(self, lpath, rpath, blocksize=5 * 2 ** 20, acl=None):
def put(self, lpath, rpath, blocksize=5 * 2 ** 20, acl=None,
metadata=None):
"""Upload local file to remote

Parameters
Expand All @@ -931,15 +933,35 @@ def put(self, lpath, rpath, blocksize=5 * 2 ** 20, acl=None):
Chunks in which the data is sent
acl: str or None
Optional access control to apply to the created object
metadata: None or dict
Gets added to object metadata on server
"""
with self.open(rpath, 'wb', block_size=blocksize, acl=acl) as f1:
with self.open(rpath, 'wb', block_size=blocksize, acl=acl,
metadata=metadata) as f1:
with open(lpath, 'rb') as f2:
while True:
d = f2.read(blocksize)
if not d:
break
f1.write(d)

def getxattr(self, path, attr):
"""Get user-defined metadata attribute"""
meta = self.info(path).get('metadata', {})
return meta[attr]

def setxattrs(self, path, **kwargs):
""" Set user-defined metadata attributes

Parameters
---------
kw_args : key-value pairs like field="value", where the values must be
strings. Replaces existing metadata.
"""
bucket, key = split_path(path)
self._call('put', "b/{}/o/{}", bucket, key,
json={'metadata': kwargs})

@_tracemethod
def head(self, path, size=1024):
""" Fetch start of file
Expand Down Expand Up @@ -1073,9 +1095,12 @@ def open(self, path, mode='rb', block_size=None, acl=None,
metadata=metadata))

@_tracemethod
def touch(self, path):
"""Create empty file"""
with self.open(path, 'wb'):
def touch(self, path, acl=None, metadata=None):
"""Create empty file

acl, metadata: passed on to open() and then GCSFile
"""
with self.open(path, 'wb', acl=acl, metadata=metadata):
pass

@_tracemethod
Expand Down Expand Up @@ -1417,8 +1442,21 @@ def _simple_upload(self):
data = self.buffer.read()
path = ('https://www.googleapis.com/upload/storage/v1/b/%s/o'
% quote_plus(self.bucket))
r = self.gcsfs.session.post(
path, params={'uploadType': 'media', 'name': self.key}, data=data)
metadata = {'name': self.key}
if self.metadata is not None:
metadata['metadata'] = self.metadata
metadata = json.dumps(metadata)
data = ('--==0=='
'\nContent-Type: application/json; charset=UTF-8'
'\n\n' + metadata +
'\n--==0=='
'\nContent-Type: application/octet-stream'
'\n\n').encode() + data + b'\n--==0==--'
r = self.gcsfs.session.post(path,
headers={
'Content-Type': 'multipart/related; boundary="==0=="'},
params={'uploadType': 'multipart'},
data=data)
validate_response(r, path)
size, md5 = int(r.json()['size']), r.json()['md5Hash']
if self.consistency == 'size':
Expand Down
1,600 changes: 57 additions & 1,543 deletions gcsfs/tests/recordings/test_array.yaml

Large diffs are not rendered by default.

Loading