def upload_resource(session, entity_id, local_path, label=None, resource_group=None, progress=tqdm): """ Upload a single local file as a Resource :param session: Session :param entity_id: owning entity UUID :param local_path: local file path :param label: Resource label :param resource_group: (optional) ResourceGroup dict :param progress: (optional) progress monitor :return: new Resource dict """ file_name = os.path.basename(local_path) content_type = upload.guess_content_type(file_name) data = {'resource': {'entity_id': entity_id, 'path': file_name}} if label: data['resource']['label'] = label if resource_group is not None: data['resource']['resource_group_id'] = resource_group.id r = session.post(session.entity_path('resources'), data=data) aws = r['aws'] upload.upload_to_aws(aws, content_type, local_path, progress) metadata = session.get(session.entity_path('resources', r.id) + "/metadata") r.version = metadata.version_id r.type = 'resource' return session.put(session.entity_path('resources', r.id), entity=r)
def copy_file(session, parent=None, file_key=None, file_name=None, source_bucket=None, destination_bucket=None, aws_access_key_id=None, aws_secret_access_key=None): """ Creates an Ovation 'File' and 'Revision' record. File is transferred from source_bucket to destination_bucket and then the Revision` version is set to S3 version_id of the object in the destination_bucket. :param session: ovation.connection.Session :param parent: Project or Folder (entity dict or ID) :param file_key: S3 Key of file to copy from source bucket :param file_name: Name of file :param source_bucket: the S3 bucket to copy file from :param destination_bucket: the destination_bucket to copy file to :param aws_access_key_id: id for key that must have read access to source and write access to destination :param aws_secret_access_key: AWS key :return: new `Revision` entity dicitonary """ content_type = upload.guess_content_type(file_name) # create file record new_file = core.create_file(session, parent, file_name, attributes={'original_s3_path': file_key}) # create revision record r = session.post(new_file.links.self, data={'entities': [{'type': 'Revision', 'attributes': {'name': file_name, 'content_type': content_type}}]}) # get key for where to copy existing file from create revision resonse revision = r['entities'][0] destination_s3_key = r['aws'][0]['aws']['key'] # Copy file from source bucket to destination location # Need to use key that has 'list' and 'get_object' privleges on source bucket as well as 'write' privleges # on destination bucket (can't use the temp aws key from the create revision response since this does # not have read access to the source bucket) aws_session = boto3.Session(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) s3 = aws_session.resource('s3') destination_file = s3.Object(destination_bucket, destination_s3_key) copy_source = "{0}/{1}".format(source_bucket, file_key) try: aws_response = destination_file.copy_from(CopySource=copy_source) # get version_id from AWS copy response and update revision record with aws version_id # revision['attributes']['version'] = aws_response['VersionId'] revision_response = session.put(revision['links']['upload-complete'], entity=None) return revision_response except Exception as e: logging.error(e, exc_info=True) return None