Closed bbelderbos closed 1 year ago
if file is not None:
addresses thisif file is not None:
addresses thisif file is not None:
addresses thisI 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
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.
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.
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
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?
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?
... also referred to as "key" - https://stackoverflow.com/a/43900919
Nvm you already coded it, let me check the code on my end ...
@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:
= old zip-strict image got deleted.
Bucket after edit:
= my new Picture 1.png got uploaded.
Delete also works:
=> Picture 1.png got deleted.
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 ...
Ah, got it. Thanks!
I really need to watch the pdb video and get used to using it.
edit treat - change image, use boto3 to delete the old image