Skip to content

Commit

Permalink
refactor: BulkDeleteHeaders function
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrokiefer authored May 9, 2020
1 parent 10cc4ab commit c094ebe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion largeobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (c *Connection) LargeObjectDelete(container string, objectName string) erro
for i, obj := range objects {
filenames[i] = obj[0] + "/" + obj[1]
}
_, err = c.doBulkDelete(filenames)
_, err = c.doBulkDelete(filenames, nil)
// Don't fail on ObjectNotFound because eventual consistency
// makes this situation normal.
if err != nil && err != Forbidden && err != ObjectNotFound {
Expand Down
34 changes: 25 additions & 9 deletions swift.go
Original file line number Diff line number Diff line change
Expand Up @@ -1909,22 +1909,26 @@ type BulkDeleteResult struct {
Headers Headers // Response HTTP headers.
}

func (c *Connection) doBulkDelete(objects []string) (result BulkDeleteResult, err error) {
func (c *Connection) doBulkDelete(objects []string, h Headers) (result BulkDeleteResult, err error) {
var buffer bytes.Buffer
for _, s := range objects {
u := url.URL{Path: s}
buffer.WriteString(u.String() + "\n")
}
extraHeaders := Headers{
"Accept": "application/json",
"Content-Type": "text/plain",
"Content-Length": strconv.Itoa(buffer.Len()),
}
for key, value := range h {
extraHeaders[key] = value
}
resp, headers, err := c.storage(RequestOpts{
Operation: "DELETE",
Parameters: url.Values{"bulk-delete": []string{"1"}},
Headers: Headers{
"Accept": "application/json",
"Content-Type": "text/plain",
"Content-Length": strconv.Itoa(buffer.Len()),
},
ErrorMap: ContainerErrorMap,
Body: &buffer,
Headers: extraHeaders,
ErrorMap: ContainerErrorMap,
Body: &buffer,
})
if err != nil {
return
Expand Down Expand Up @@ -1964,6 +1968,18 @@ func (c *Connection) doBulkDelete(objects []string) (result BulkDeleteResult, er
// * http://docs.openstack.org/trunk/openstack-object-storage/admin/content/object-storage-bulk-delete.html
// * http://docs.rackspace.com/files/api/v1/cf-devguide/content/Bulk_Delete-d1e2338.html
func (c *Connection) BulkDelete(container string, objectNames []string) (result BulkDeleteResult, err error) {
return c.BulkDeleteHeaders(container, objectNames, nil)
}

// BulkDeleteHeaders deletes multiple objectNames from container in one operation.
//
// Some servers may not accept bulk-delete requests since bulk-delete is
// an optional feature of swift - these will return the Forbidden error.
//
// See also:
// * http://docs.openstack.org/trunk/openstack-object-storage/admin/content/object-storage-bulk-delete.html
// * http://docs.rackspace.com/files/api/v1/cf-devguide/content/Bulk_Delete-d1e2338.html
func (c *Connection) BulkDeleteHeaders(container string, objectNames []string, h Headers) (result BulkDeleteResult, err error) {
if len(objectNames) == 0 {
result.Errors = make(map[string]error)
return
Expand All @@ -1972,7 +1988,7 @@ func (c *Connection) BulkDelete(container string, objectNames []string) (result
for i, name := range objectNames {
fullPaths[i] = fmt.Sprintf("/%s/%s", container, name)
}
return c.doBulkDelete(fullPaths)
return c.doBulkDelete(fullPaths, h)
}

// BulkUploadResult stores results of BulkUpload().
Expand Down

0 comments on commit c094ebe

Please sign in to comment.