Open aislanmaia opened 7 years ago
I'm pretty sure you can delete object by doing Api.File.delete({activity.file, activity})
(might be wrong, but thats how i've been doing it)
Not sure if that makes a difference..
The problem could also be related to the UUID juggling you are doing, when you update your activity
using put_change(:uuid, UUID.uuid4())
, it might leave the uploads on an unreachable path, since the UUID by which the file was stored has since been overwritten?
ps: you can tag your code blocks with elixir, to make them easier to read start the block like this: ```elixir
Thanks @pedep I already have accomplished to get the delete process working just fine some time ago. The problem with :uuid
was real... exactly what you said, and because that, I added a new changeset
function only to update process excluding the call to put_change
function from the pipeline. Now I have a changeset
for inserts and update_changeset
for updates. However, the code part for deleting a file, actually my code is a little different to what I was doing above. Here is my delete function:
def delete_file(file, activity) do
if file do
# Deletes the preview file uploaded so then we wont have
# orphan files in S3 storage.
list = ExAws.S3.list_objects(System.get_env("AWS_S3_BUCKET_UPLOADS"), prefix: "uploads/activities")
|> ExAws.stream!
|> Enum.to_list #=> [list, of, objects]
Enum.each list, fn obj ->
[_, _, _, file_name] = String.split obj.key, "/"
if file_name == activity.file.file_name do
ExAws.S3.delete_object(System.get_env("AWS_S3_BUCKET_UPLOADS"), obj.key)
|> ExAws.request()
end
end
end
Just giving my reply here for helping others that get trapped in the same problem with :uuid
thing and the deleting business.
And thanks for the tips regarding the elixir tag
and remember me this issue I have created exists :smile: so I can close it :laughing:
Ah, great :smiley: Good to see you got it working, and to hear my intuition wasn't compelely off :smile:
I'm trying to delete a file on S3 but don't seems working right now.
Below is my bit of code, and I'm trying to get the object deleted right there in the delete function on controller. (using Phoenix 1.2)
The weird thing is that trying to use the almost same bit of code in the update function, the object in S3 gets replaced by a new one (working like expected, the code first delete on S3, and the library adds the new one on the params)
Here is my Activity schema:
A detail is that I'm using sub directories to storage matching the scope: