Skip to Content
Menú
This question has been flagged
1 Respondre
1538 Vistes

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})
Avatar
Descartar
Best Answer

Hey, 


Check this module may help you:

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

Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
de gen. 23
6151
1
de des. 22
2468
0
de maig 22
907
0
de jul. 22
2243
0
de febr. 22
1907