Search code examples
python-3.xamazon-web-servicesboto3cloud9

Python - Finding path of a file on cloud9 AWS (boto3)


I am trying to put an object inside a S3 bucket from the cloud9 environment The file is in the same folder inside the IDE. I have a folder called project1 and this script and the file are in the same folder This is my code

 import logging
    import boto3
    from botocore.exceptions import ClientError


def put_object(dest_bucket_name, dest_object_name, src_data):
    """
    Add an object to an Amazon S3 bucket

    The src_data argument must be of type bytes or a string that references
    a file specification.

    :param dest_bucket_name: string
    :param dest_object_name: string
    :param src_data: bytes of data or string reference to file spec
    :return: True if src_data was added to dest_bucket/dest_object, otherwise
    False
    """

    # Construct Body= parameter
    if isinstance(src_data, bytes):
        object_data = src_data
    elif isinstance(src_data, str):
        try:
            object_data = open(src_data, 'rb')
            # possible FileNotFoundError/IOError exception
        except Exception as e:
            logging.error(e)
            return False
    else:
        logging.error('Type of ' + str(type(src_data)) +
                      ' for the argument \'src_data\' is not supported.')
        return False

    # Put the object
    s3 = boto3.client('s3')
    try:
        s3.put_object(Bucket=dest_bucket_name, Key=dest_object_name, Body=object_data)
    except ClientError as e:
        # AllAccessDisabled error == bucket not found
        # NoSuchKey or InvalidRequest error == (dest bucket/obj == src bucket/obj)
        logging.error(e)
        return False
    finally:
        if isinstance(src_data, str):
            object_data.close()
    return True


def main():
    """Exercise put_object()"""

    test_bucket_name = 'BUCKET_NAME'
    test_object_name = 'OBJECT_NAME'
    filename = 'file.ext'
    # Alternatively, specify object contents using bytes.
    # filename = b'This is the data to store in the S3 object.'

    # Set up logging
    logging.basicConfig(level=logging.DEBUG,
                        format='%(levelname)s: %(asctime)s: %(message)s')

    # Put the object into the bucket
    success = put_object(test_bucket_name, test_object_name, filename)
    if success:
        logging.info(f'Added {test_object_name} to {test_bucket_name}')


if __name__ == '__main__':
    main()

Is it possible to put the file from the IDE to the S3 bucket?


Solution

  • Is it possible to put the file from the IDE to the S3 bucket?

    Yes. If you know its relative path, you can use regular boto3's method to upload the file to s3, assuming you have permissions to access s3.

    If you are not sure what is you current working directory in your scripts, you can use

    import os
    
    print(os.getcwd())
    

    Obviously you can use os.getcwd() to build up full path to your script, e.g.:

    os.getcwd()/file/to/upload/to/s3.txt