[英]How to make objects public through python code once uplaoded to s3 bucket from ubuntu system using boto3?
[英]Amazon S3 boto3 how to iterate through objects in a bucket?
在my_bucket.objects.all()
應用程序中,我試圖遍歷 S3 存儲桶中的對象並嘗試打印密鑰/文件名,但my_bucket.objects.all()
僅返回存儲桶中的第一個對象。 它沒有返回所有對象。 輸出是[001.pdf]
而不是[001, 002, 003, 004, 005]
from flask import Flask, jsonify, Response, request
from flask_cors import CORS, cross_origin
from config import S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_ACCESS_KEY
import boto3
import csv
import re
s3 = boto3.client(
's3',
aws_access_key_id=S3_ACCESS_KEY,
aws_secret_access_key=S3_SECRET_ACCESS_KEY
)
app = Flask(__name__)
CORS(app, supports_credentials=True)
@app.route('/')
def health():
return jsonify({"message": "app is working"})
@app.route('/files')
def list_of_files():
s3_resource = boto3.resource('s3')
my_bucket = s3_resource.Bucket(S3_BUCKET)
summaries = my_bucket.objects.all()
files = []
for file in summaries:
# this prints the bucket object
print("Object: {}".format(summaries))
files.append(file.key)
# file.key is supposed to return the names of the list of objects
# print(file.key)
return jsonify({"files":"{}".format(file.key)})
if __name__ == "__main__":
app.run()
您過早返回而退出循環。
def list_of_files():
s3_resource = boto3.resource('s3')
my_bucket = s3_resource.Bucket(S3_BUCKET)
summaries = my_bucket.objects.all()
files = []
for file in summaries:
files.append(file.key)
return jsonify({"files": files})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.