跳至内容
菜单
此问题已终结
1 回复
1972 查看

I'm trying to upload all the product images to s3 bucket and save the link ,the process is working so far ,but the image uploaded is unreadable  

here is my code :

def update_product_images(self):
# Initialize the S3 client
s3 = boto3.client('s3',
aws_access_key_id='aws_key',
aws_secret_access_key='aws_secret_key',
region_name='region_name'
)
bucket = "bucket_name"

# Retrieve all products
products = self.env['product.template'].search([])

for product in products:
if product.image_1920: # Assuming image field contains the image binary data
# Generate a unique S3 key for the image
s3_key = f'product_images/{product\.id\}_\{product\.name\}\.png'
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ s3_key_encoded\ =\ quote\(s3_key\)
\
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ image_fileobj\ =\ BytesIO\(product\.image_1920\)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \#\ Upload\ the\ image\ to\ S3
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ s3\.upload_fileobj\(image_fileobj,\ bucket,\ s3_key_encoded,ExtraArgs=\{'ContentType':\ 'image/png'\}\)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \#\ Upload\ the\ image\ to\ S3
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \#\ s3\.put_object\(Bucket=bucket,Key=s3_key_encoded,Body=image_data,ContentType="image/png"\)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \#\ Construct\ the\ S3\ image\ URL
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ s3_url\ =\ f'https://\{bucket\}.s3.amazonaws.com/{s3_key_encoded}'

# Update the product record with the S3 image URL
product.write({'image_url': s3_url})
形象
丢弃
最佳答案

Hey, 


Check this module may help you:

apps.odoo.com/apps/modules/17.0/aws_s3_auto_attachments

形象
丢弃
相关帖文 回复 查看 活动
1
1月 23
7087
1
12月 22
2903
0
5月 22
1192
0
7月 22
2547
0
2月 22
2218