Get retrieves the stored value for the given key. You need to pass a pointer to the value, so in case of a struct the automatic unmarshalling can populate the fields of the object that v points to with the values of the retrieved object's values. If no value is found it returns (false, nil). The key
(k string, v any)
| 56 | // If no value is found it returns (false, nil). |
| 57 | // The key must not be "" and the pointer must not be nil. |
| 58 | func (c Client) Get(k string, v any) (found bool, err error) { |
| 59 | if err := util.CheckKeyAndValue(k, v); err != nil { |
| 60 | return false, err |
| 61 | } |
| 62 | |
| 63 | getObjectInput := awss3.GetObjectInput{ |
| 64 | Bucket: &c.bucketName, |
| 65 | Key: &k, |
| 66 | } |
| 67 | getObjectOutput, err := c.c.GetObject(&getObjectInput) |
| 68 | if err != nil { |
| 69 | aerr, ok := err.(awserr.Error) |
| 70 | if ok && aerr.Code() == awss3.ErrCodeNoSuchKey { |
| 71 | return false, nil |
| 72 | } |
| 73 | return false, err |
| 74 | } |
| 75 | if getObjectOutput.Body == nil { |
| 76 | // Return false if there's no value |
| 77 | // TODO: Maybe return an error? Behaviour should be consistent across all implementations. |
| 78 | return false, nil |
| 79 | } |
| 80 | data, err := ioutil.ReadAll(getObjectOutput.Body) |
| 81 | if err != nil { |
| 82 | return true, err |
| 83 | } |
| 84 | |
| 85 | return true, c.codec.Unmarshal(data, v) |
| 86 | } |
| 87 | |
| 88 | // Delete deletes the stored value for the given key. |
| 89 | // Deleting a non-existing key-value pair does NOT lead to an error. |