Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
1965 Weergaven

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
Annuleer
Beste antwoord

Hey, 


Check this module may help you:

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

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
jan. 23
7064
1
dec. 22
2896
0
mei 22
1185
0
jul. 22
2532
0
feb. 22
2210