adamdoty / homebaker

A baked-goods, birthday-treat, tracking app I made for my wife.
3 stars 0 forks source link

[baker] sync S3 bucket #50

Closed bbelderbos closed 1 year ago

bbelderbos commented 1 year ago

edit treat - change image, use boto3 to delete the old image

adamdoty commented 1 year ago

I settled on adding this if stmt within the if file is not None: block. if treat.cover_img: ___delete_from_s3(file) This addressed case 3, and without it we would be trying to delete image from the s3 bucket that didnt exist

adamdoty commented 1 year ago

I think I found the right method in the docs

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html?highlight=delete#S3.Bucket.delete https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html?highlight=delete#S3.Object.delete

adamdoty commented 1 year ago

def treat_edit(request, pk): ____treat = get_object_or_404(Treat, pk=pk, user=request.user)

I don't recall the logic of why we had user=request.user. I removed it, and the coupon_edit view is at least showing the template now.

adamdoty commented 1 year ago

hmmm..... I wonder if this would be simpler if we stored the aws response when we create and then reference it if we want to delete it

I'm not quite sure how to reverse get the aws response for the image in the bucket from the url, but still working on it.

adamdoty commented 1 year ago

trying to add feedback to the user into the upload image field when editing a treat currently, there is no indication that an image already exists for the treat

would be nice to present the image url and change the img_upload label to say "Update an Image" see lines 95-97 of views.py

adamdoty commented 1 year ago

I think I was able to figure out how to delete the objects from the s3 bucket.

However, I manually deleted all the images from my s3 bucket, yet the images still appear in the list view or if you reference the url. I found this stackoverflow post about versions, but my bucket has versions disabled.

Could it be that there is lag time between when the images are deleted from the management console and when they get deleted from the server?

bbelderbos commented 1 year ago

That's odd! Did you manage to delete images with boto3 programmatically? You can pick the base file name from the url stored in the database right?

bbelderbos commented 1 year ago

... also referred to as "key" - https://stackoverflow.com/a/43900919

bbelderbos commented 1 year ago

Nvm you already coded it, let me check the code on my end ...

bbelderbos commented 1 year ago

@adamdoty hm "edit treat" works fine for me, I used debugger to look at AWS response of s3.Object(s3_bucket, key).delete():

{'ResponseMetadata': {'RequestId': 'K09Y3JQXM2WX3CRQ', 'HostId': 'pWvnUgrdyawZO+GIxkp0lUzhmztKZK6qA44Eimcsss93kaXzmwFRn/Ao4q1kmuU13gr4/noopSA=', 'HTTPStatusCode': 204, 'HTTPHeaders': {'x-amz-id-2': 'pWvnUgrdyawZO+GIxkp0lUzhmztKZK6qA44Eimcsss93kaXzmwFRn/Ao4q1kmuU13gr4/noopSA=', 'x-amz-request-id': 'K09Y3JQXM2WX3CRQ', 'date': 'Fri, 07 Oct 2022 11:03:47 GMT', 'server': 'AmazonS3'}, 'RetryAttempts': 1}}

= 204 = HTTP code for delete = ok.

Bucket before edit:

Screenshot 2022-10-07 at 13 04 11

= old zip-strict image got deleted.

Bucket after edit:

Screenshot 2022-10-07 at 13 04 23

= my new Picture 1.png got uploaded.

bbelderbos commented 1 year ago

Delete also works:

image

=> Picture 1.png got deleted.

bbelderbos commented 1 year ago

I did hit a cache issue now, shift refresh, the image did not show on page anymore. Best to check your bucket like I did above ...

adamdoty commented 1 year ago

Ah, got it. Thanks!

adamdoty commented 1 year ago

I really need to watch the pdb video and get used to using it.