Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
1946 Tampilan

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
Buang
Jawaban Terbai

Hey, 


Check this module may help you:

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

Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
1
Jan 23
7040
1
Des 22
2881
0
Mei 22
1165
0
Jul 22
2507
0
Feb 22
2198