Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
1 Antworten
1901 Ansichten

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
Verwerfen
Beste Antwort

Hey, 


Check this module may help you:

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

Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
1
Jan. 23
6920
1
Dez. 22
2823
0
Mai 22
1140
0
Juli 22
2479
0
Feb. 22
2164