storj / uplink

Storj network Go library
MIT License
115 stars 18 forks source link

[QUESTION] Delete method #162

Open JonatanAlcaraz opened 7 months ago

JonatanAlcaraz commented 7 months ago

Is there any method to delete objects from a bucket by key without deleting the whole bucket?

Thanks

egonelbre commented 7 months ago

Yes, there's DeleteObject https://pkg.go.dev/storj.io/uplink#Project.DeleteObject.

JonatanAlcaraz commented 7 months ago

Thanks for your reply @egonelbre

I was trying to integrate it but the result I get is always a null object but at the same time it does not return error. The steps I follow are the following:

        access, err := uplink.ParseAccess(s.AccessGrant)
    if err != nil {
        fmt.Println(err)
    }
        if err != nil {
        return fmt.Errorf("could not generate access: %v", err)
    }
    project, err := uplink.OpenProject(ctx, access)
    if err != nil {
        return fmt.Errorf("could not open project: %v", err)
    }
    // here I try to execute the method with the object name
        deleted, err := project.DeleteObject(ctx, s.BucketName, objectKey)

        // here I try to execute the method with the path of the object 
    key := fmt.Sprintf("%s%s", s.ObjectPath, objectKey)
    deleted, err := project.DeleteObject(ctx, s.BucketName, key)

Just in case after the process I checked if the file is still inside the bucket and it remains in the bucket. In addition, when generating the "access garant"in the storj dashboard, I set the Read, Write, List and Delete permissions.

egonelbre commented 7 months ago

Yeah, DeleteObject is somewhat weird in that sense that it doesn't return an error when the object doesn't exist.

But, a common bug is to use the wrong object key, e.g. accidentally using /image.jpg instead of image.jpg. You can use ListObjects to print out things in the bucket and see what you get.

Another common bug is to use different passphrases in your code and dashboard -- in which case the encrypted object keys won't match.