【问题标题】:Uploading CSV files to S3 directly through Django通过 Django 直接将 CSV 文件上传到 S3
【发布时间】:2020-11-14 06:43:54
【问题描述】:

我正在创建一个连接到 Amazon S3 以保存用户上传文件的 django 应用程序。这是使用 django-storages 完成的。

在应用过程中会生成一个 CSV 文件。我可以在本地保存文件。但是如何在不保存本地的情况下直接将文件上传到S3,同时将文件S3 URL保存在数据库中呢?

我曾尝试使用 StringIO,但会产生 TypeError: Unicode objects must be encrypted before hashing

任何建议/解决方案将不胜感激!

【问题讨论】:

    标签: django amazon-s3 boto3 botocore python-django-storages


    【解决方案1】:
    // Install boto3 first
    import boto3
    
    s3 = boto3.client('s3',
                      aws_access_key_id=AWS_ACCESS_KEY_ID,
                      aws_secret_access_key=AWS_SECRET_ACCESS_KEY, )
    
    // ...... Get your file or open file ......
    file_name = your_file_here
    // ......
    
    s3.upload_file(file_name, AWS_STORAGE_BUCKET_NAME,
                   path_where_you_want_to_store))
    
    // Ex: s3.upload_file(file_name, AWS_STORAGE_BUCKET_NAME,
    //                           '%s/%s' % ('media/user/file', file_name))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-11
      • 2021-02-02
      • 2018-12-15
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      相关资源
      最近更新 更多