如何使用Python中的Boto检查文件是否已完成上传到S3 Bucket?

库兹切斯

我正在尝试使用Boto将图像上传到S3存储桶中。图像成功上传后,我想使用S3存储桶中图像的文件URL进行某些操作。问题是,有时图像上传速度不够快,并且当我要执行依赖于图像文件URL的操作时,服务器最终会报错。

这是我的源代码。我正在使用python flask。

def search_test(consumer_id):

consumer = session.query(Consumer).filter_by(consumer_id=consumer_id).one()
products = session.query(Product).all()
product_dictionary = {'Products': [p.serialize for p in products]}

if request.method == 'POST':
    p_product_image_url = request.files['product_upload_url']
    s3 = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    bucket = s3.get_bucket(AWS_BUCKET_NAME)
    k = Key(bucket)
    if p_product_image_url and allowed_file(p_product_image_url.filename):

        # Read the contents of the file
        file_content = p_product_image_url.read()

        # Use Boto to upload the file to S3
        k.set_metadata('Content-Type', mimetypes.guess_type(p_product_image_url.filename))
        k.key = secure_filename(p_product_image_url.filename)
        k.set_contents_from_string(file_content)
        print ('consumer search upload successful')

    new_upload = Uploads(picture_upload_url=k.key.replace(' ', '+'), consumer=consumer)
    session.add(new_upload)
    session.commit()

    new_result = jsonify(Result=perform_actual_search(amazon_s3_base_url + k.key.replace(' ', '+'),

                                                      product_dictionary))

    return new_result
else:
    return render_template('upload_demo.html', consumer_id=consumer_id)

jsonify方法需要一个有效的图像URL来执行该操作。有时它起作用,有时却不起作用。我怀疑这是由于该图像在执行该行代码时尚未上传的问题所致。

perform_actual_search方法如下:

def get_image_search_results(image_url):
global description
url = ('http://style.vsapi01.com/api-search/by-url/?apikey=%s&url=%s' % (just_visual_api_key, image_url))
h = httplib2.Http()
response, content = h.request(url,
                              'GET')  # alternatively write content=h.request((url,'GET')[1]) ///Numbr 2 in our array
result = json.loads(content)

result_dictionary = []

for i in range(0, 10):
    if result:
        try:
            if result['errorMessage']:
                result_dictionary = []
        except:
            pass

            if result['images'][i]:
                images = result['images'][i]
                jv_img_url = images['imageUrl']
                title = images['title']
                try:
                    if images['description']:
                        description = images['description']
                    else:
                        description = "no description"
                except:
                    pass

                # print("\njv_img_url: %s,\ntitle: %s,\ndescription: %s\n\n"% (
                # jv_img_url, title, description))

                image_info = {
                    'image_url': jv_img_url,
                    'title': title,
                    'description': description,
                }
                result_dictionary.append(image_info)

if result_dictionary != []:
    # for i in range(len(result_dictionary)):
    #     print (result_dictionary[i])
    #     print("\n\n")
    return result_dictionary
else:
    return []


def performSearch(jv_input_dictionary, imagernce_products_dict):
print jv_input_dictionary
print imagernce_products_dict

global common_desc_ratio
global isReady
image_search_results = []
if jv_input_dictionary != []:
    for i in range(len(jv_input_dictionary)):
        print jv_input_dictionary[i]
        for key in jv_input_dictionary[i]:
            if key == 'description':
                input_description = jv_input_dictionary[i][key]
                s1w = re.findall('\w+', input_description.lower())
                s1count = Counter(s1w)
                print input_description
                for j in imagernce_products_dict:
                    if j == 'Products':
                        for q in range(len(imagernce_products_dict['Products'])):
                            for key2 in imagernce_products_dict['Products'][q]:
                                if key2 == 'description':
                                    search_description = imagernce_products_dict['Products'][q]['description']
                                    print search_description
                                    s2w = re.findall('\w+', search_description.lower())
                                    s2count = Counter(s2w)
                                    # Commonality magic
                                    common_desc_ratio = difflib.SequenceMatcher(None, s1w, s2w).ratio()
                                    print('Common ratio is: %.2f' % common_desc_ratio)

                            if common_desc_ratio > 0.09:
                                image_search_results.append(imagernce_products_dict['Products'][q])

if image_search_results:

    print image_search_results
    return image_search_results
else:
    return {'404': 'No retailers registered with us currently own this product.'}


def perform_actual_search(image_url, imagernce_product_dictionary):
return performSearch(get_image_search_results(image_url), imagernce_product_dictionary)

任何解决此问题的帮助将不胜感激。

塞巴斯蒂安·斯托马克

我将S3配置为生成有关事件的通知,例如s3:ObjectCreated:*

通知可以发布到SNS主题,SQS队列或直接触发lambda函数。

有关S3通知的更多详细信息:http : //docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html

您应该重写代码以将上传部分和图像处理部分分开。后者可以在Python中实现为Lambda函数。
在这里,以异步方式工作是关键,编写阻塞代码通常是不可扩展的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Amazon AWS Cognito和Python Boto3建立AWS连接并将文件上传到Bucket

来自分类Dev

无法使用回形针导轨上传到 S3 5.无效的配置选项:bucket

来自分类Dev

如何在KMS SSE中使用boto的s3 bucket.copy_key?

来自分类Dev

如何使用boto3在同一个bucket(不同前缀)中下载和上传s3对象

来自分类Dev

将文件从 S3 Bucket 传输到另一个保存文件夹结构 - python boto

来自分类Dev

需要使用 c# 实现 AWS S3 Bucket 文件上传。而文件大小没有限制

来自分类Dev

列出AWS Bucket中的对象

来自分类Dev

在Google Bucket中创建目录

来自分类Dev

gsutil rsync bucket1 bucket2是否在本地下载/上传文件?

来自分类Dev

如何使用AWS资源安排'aws s3 sync s3:// bucket1 s3:// bucket2'?

来自分类Dev

上传到GCP存储或使用bucket.getFiles()之后,将文件名保存在数据库中

来自分类Dev

当文件在本地存储中时,如何使用boto3将文件上传到s3

来自分类Dev

使用boto3将文件上传到S3中的特定文件夹

来自分类Dev

boto2 是否使用 http 或 https 将文件上传到 s3?

来自分类Dev

“ Bucket”在python中是什么意思?

来自分类Dev

如何使用hadoop fs -cp s3:// <bucket> hdfs:/// tmp

来自分类Dev

回形针/ S3的“缺少必需的:bucket选项”

来自分类Dev

在S3 AWS Bucket上启用MFA删除

来自分类Dev

object exists in AWS S3 bucket in Node.JS

来自分类Dev

将S3 Bucket与Dynamodb Android链接?

来自分类Dev

AWS S3权限-put-bucket-acl错误

来自分类Dev

如何与GSUTIL共享整个Google Cloud Bucket

来自分类Dev

在bucket.createReadStream中找不到错误

来自分类Dev

如何知道对象是否已完全上传到Amazon S3?

来自分类Dev

boto3.Bucket.upload_file是阻止还是非阻止?

来自分类Dev

Cloudformation LayerVersion S3Bucket使用参数

来自分类Dev

直接从 Heroku 的 Flask 应用程序将文件写入 S3 Bucket

来自分类Dev

get_bucket()为我未通过Boto创建的S3存储桶提供了“错误请求”

来自分类Dev

如何修复s3 / paperclip / rails模板错误:bucket选项丢失?

Related 相关文章

  1. 1

    Amazon AWS Cognito和Python Boto3建立AWS连接并将文件上传到Bucket

  2. 2

    无法使用回形针导轨上传到 S3 5.无效的配置选项:bucket

  3. 3

    如何在KMS SSE中使用boto的s3 bucket.copy_key?

  4. 4

    如何使用boto3在同一个bucket(不同前缀)中下载和上传s3对象

  5. 5

    将文件从 S3 Bucket 传输到另一个保存文件夹结构 - python boto

  6. 6

    需要使用 c# 实现 AWS S3 Bucket 文件上传。而文件大小没有限制

  7. 7

    列出AWS Bucket中的对象

  8. 8

    在Google Bucket中创建目录

  9. 9

    gsutil rsync bucket1 bucket2是否在本地下载/上传文件?

  10. 10

    如何使用AWS资源安排'aws s3 sync s3:// bucket1 s3:// bucket2'?

  11. 11

    上传到GCP存储或使用bucket.getFiles()之后,将文件名保存在数据库中

  12. 12

    当文件在本地存储中时,如何使用boto3将文件上传到s3

  13. 13

    使用boto3将文件上传到S3中的特定文件夹

  14. 14

    boto2 是否使用 http 或 https 将文件上传到 s3?

  15. 15

    “ Bucket”在python中是什么意思?

  16. 16

    如何使用hadoop fs -cp s3:// <bucket> hdfs:/// tmp

  17. 17

    回形针/ S3的“缺少必需的:bucket选项”

  18. 18

    在S3 AWS Bucket上启用MFA删除

  19. 19

    object exists in AWS S3 bucket in Node.JS

  20. 20

    将S3 Bucket与Dynamodb Android链接?

  21. 21

    AWS S3权限-put-bucket-acl错误

  22. 22

    如何与GSUTIL共享整个Google Cloud Bucket

  23. 23

    在bucket.createReadStream中找不到错误

  24. 24

    如何知道对象是否已完全上传到Amazon S3?

  25. 25

    boto3.Bucket.upload_file是阻止还是非阻止?

  26. 26

    Cloudformation LayerVersion S3Bucket使用参数

  27. 27

    直接从 Heroku 的 Flask 应用程序将文件写入 S3 Bucket

  28. 28

    get_bucket()为我未通过Boto创建的S3存储桶提供了“错误请求”

  29. 29

    如何修复s3 / paperclip / rails模板错误:bucket选项丢失?

热门标签

归档