有没有一种简单的方法可以通过python从S3获取文件?

时间:2019-10-24 16:44:54

标签: python amazon-s3

我试图创建一个脚本,该脚本从S3中的特定存储桶获取文件,
例如:来自There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:280) at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:254) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528) at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ,我想获取veryCoolFile.img
 最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)

Python拥有boto3,该API的API可以执行大部分(如果不是全部)AWS中的操作。 下一个代码可以完成您的任务:

def get_file_fore_s3(bucket,file_name):
    AWS_ACCESS_KEY_ID = <YOUR_ACCESS_KEY_ID>
    AWS_SECRET_ACCESS_KEY = <YOUR_AWS_SECRET_ACCESS_KEY>
    s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
    s3_resource = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
    all_objects = s3_client.list_objects(Bucket= bucket)
    for object in all_objects['Contents']:
        current_file_name = object['Key]
        if file_name == current_file_name:
            f = open(file_name, "w+")
            f.close()
            s3_resource.meta.client.download_file(bucket, file_name,file_name)

解决方案背后的想法是获取bucket元素(由我的代码中的all_objects描述) 并遍历存储桶的“内容”。