Esempio n. 1
0
def upload_image_to_s3(
    # See https://github.com/python/typeshed/issues/2706
    bucket: ServiceResource,
    file_name: str,
    content_type: Optional[str],
    user_profile: UserProfile,
    contents: bytes,
) -> None:
    key = bucket.Object(file_name)
    metadata = {
        "user_profile_id": str(user_profile.id),
        "realm_id": str(user_profile.realm_id),
    }

    content_disposition = ""
    if content_type is None:
        content_type = ""
    if content_type not in INLINE_MIME_TYPES:
        content_disposition = "attachment"

    key.put(
        Body=contents,
        Metadata=metadata,
        ContentType=content_type,
        ContentDisposition=content_disposition,
    )
Esempio n. 2
0
    def delete_file_from_s3(self, path_id: str, bucket: ServiceResource) -> bool:
        key = bucket.Object(path_id)

        try:
            key.load()
        except botocore.exceptions.ClientError:
            file_name = path_id.split("/")[-1]
            logging.warning("%s does not exist. Its entry in the database will be removed.", file_name)
            return False
        key.delete()
        return True
Esempio n. 3
0
def read_s3(bucket: str, key: str, s3: ServiceResource = None):
    """
    returns file
    :type bucket: str
    :param bucket:

    :type key: str
    :param key:
    :param s3: S3 resource

    :return: str
    """
    if not s3:
        log.warning('creating a S3 resource in read_s3() function')
        s3 = get_s3_resource()
    t0 = time()
    log.info("Downloading config file {0} from s3://{1}...".format(
        key, bucket))
    obj = s3.Object(bucket, key)
    log.debug('ET for reading {} from S3: {} sec'.format(
        key, round(time() - t0, 4)))
    return obj.get()['Body'].read().decode('utf-8')