def create_ocm_share(self, grantee_opaque, idp, domain, endpoint, file_path, grantee_type, role, reshare): opaque = cs3_types.Opaque( map={ "permissions": cs3_types.OpaqueEntry(decoder="plain", value=str.encode(self._map_role(role))), "name": cs3_types.OpaqueEntry(decoder="plain", value=str.encode('my_resource_name')) }) resource_id = storage_resources.ResourceId(storage_id=endpoint, opaque_id=file_path) user_id = identity_res.UserId(idp=idp, opaque_id=grantee_opaque) grantee_opaque = storage_resources.Grantee( type=ShareUtils.map_grantee(grantee_type), user_id=user_id) perms = sharing_res.SharePermissions( permissions=ShareUtils.get_resource_permissions(role), reshare=bool(reshare)) grant = sharing_res.ShareGrant(permissions=perms, grantee=grantee_opaque) provider_info = self._get_provider_info(domain) request = ocm_api.CreateOCMShareRequest( opaque=opaque, resource_id=resource_id, grant=grant, recipient_mesh_provider=provider_info) response = self.ocm_share_api.CreateOCMShare(request=request, metadata=self._token()) if self._is_code_ok(response): self.log.info("OCM share created:\n" + response) else: self._handle_error(response, "Error creating OCM share") return self._map_share(response.share)
def writefile(_endpoint, filepath, userid, content, islock=False): '''Write a file using the given userid as access token. The entire content is written and any pre-existing file is deleted (or moved to the previous version if supported). The islock flag is currently not supported. TODO the backend should at least support writing the file with O_CREAT|O_EXCL flags to prevent races.''' if islock: ctx['log'].warning( 'msg="Lock (no-overwrite) flag not yet supported, going for standard upload"' ) tstart = time.time() # prepare endpoint if isinstance(content, str): content = bytes(content, 'UTF-8') size = str(len(content)) metadata = types.Opaque( map={ "Upload-Length": types.OpaqueEntry(decoder="plain", value=str.encode(size)) }) req = cs3sp.InitiateFileUploadRequest(ref=cs3spr.Reference(path=filepath), opaque=metadata) initfileuploadres = ctx['cs3stub'].InitiateFileUpload( request=req, metadata=[('x-access-token', userid)]) if initfileuploadres.status.code != cs3code.CODE_OK: ctx['log'].debug('msg="Failed to initiateFileUpload on write" filepath="%s" reason="%s"' % \ (filepath, initfileuploadres.status.message)) raise IOError(initfileuploadres.status.message) ctx['log'].debug( 'msg="writefile: InitiateFileUploadRes returned" protocols="%s"' % initfileuploadres.protocols) # Upload try: # Get the endpoint for simple protocol # TODO: configure TUS client protocol = [ p for p in initfileuploadres.protocols if p.protocol == "simple" ][0] headers = { 'x-access-token': userid, 'Upload-Length': size, 'X-Reva-Transfer': protocol. token # needed if the uploads pass through the data gateway in reva } putres = requests.put(url=protocol.upload_endpoint, data=content, headers=headers) except requests.exceptions.RequestException as e: ctx['log'].error( 'msg="Exception when uploading file to Reva" reason="%s"' % e) raise IOError(e) tend = time.time() if putres.status_code != http.client.OK: ctx['log'].error( 'msg="Error uploading file to Reva" code="%d" reason="%s"' % (putres.status_code, putres.reason)) raise IOError(putres.reason) ctx['log'].info('msg="File written successfully" filepath="%s" elapsedTimems="%.1f" islock="%s"' % \ (filepath, (tend-tstart)*1000, islock))
def write_file(self, file_path, content, endpoint=None): """ Write a file using the given userid as access token. The entire content is written and any pre-existing file is deleted (or moved to the previous version if supported). """ # # Prepare endpoint # time_start = time.time() reference = file_utils.get_reference(file_path, endpoint) if isinstance(content, str): content_size = str(len(content)) else: content_size = str(len(content.decode('utf-8'))) meta_data = types.Opaque( map={ "Upload-Length": types.OpaqueEntry(decoder="plain", value=str.encode(content_size)) }) req = cs3sp.InitiateFileUploadRequest(ref=reference, opaque=meta_data) init_file_upload_res = self.cs3_api.InitiateFileUpload( request=req, metadata=[('x-access-token', self.auth.authenticate())]) if init_file_upload_res.status.code != cs3code.CODE_OK: self.log.debug('msg="Failed to initiateFileUpload on write" file_path="%s" reason="%s"' % \ (file_path, init_file_upload_res.status.message)) raise IOError(init_file_upload_res.status.message) self.log.debug( 'msg="writefile: InitiateFileUploadRes returned" protocols="%s"' % init_file_upload_res.protocols) # # Upload # try: protocol = [ p for p in init_file_upload_res.protocols if p.protocol == "simple" ][0] headers = { 'Tus-Resumable': '1.0.0', 'File-Path': file_path, 'File-Size': content_size, 'x-access-token': self.auth.authenticate(), 'X-Reva-Transfer': protocol.token } put_res = requests.put(url=protocol.upload_endpoint, data=content, headers=headers) except requests.exceptions.RequestException as e: self.log.error( 'msg="Exception when uploading file to Reva" reason="%s"' % e) raise IOError(e) time_end = time.time() if put_res.status_code != http.HTTPStatus.OK: self.log.error( 'msg="Error uploading file to Reva" code="%d" reason="%s"' % (put_res.status_code, put_res.reason)) raise IOError(put_res.reason) self.log.info( 'msg="File open for write" filepath="%s" elapsedTimems="%.1f"' % (file_path, (time_end - time_start) * 1000))