| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // CopyAndWait copies a blob to a destination within the storage account and waits for it to finish copying. |
| 10 | func (client Client) CopyAndWait(ctx context.Context, accountName, containerName, blobName string, input CopyInput, pollingInterval time.Duration) error { |
| 11 | if _, err := client.Copy(ctx, accountName, containerName, blobName, input); err != nil { |
| 12 | return fmt.Errorf("Error copying: %s", err) |
| 13 | } |
| 14 | |
| 15 | for true { |
| 16 | getInput := GetPropertiesInput{ |
| 17 | LeaseID: input.LeaseID, |
| 18 | } |
| 19 | getResult, err := client.GetProperties(ctx, accountName, containerName, blobName, getInput) |
| 20 | if err != nil { |
| 21 | return fmt.Errorf("") |
| 22 | } |
| 23 | |
| 24 | switch getResult.CopyStatus { |
| 25 | case Aborted: |
| 26 | return fmt.Errorf("Copy was aborted: %s", getResult.CopyStatusDescription) |
| 27 | |
| 28 | case Failed: |
| 29 | return fmt.Errorf("Copy failed: %s", getResult.CopyStatusDescription) |
| 30 | |
| 31 | case Success: |
| 32 | return nil |
| 33 | |
| 34 | case Pending: |
| 35 | time.Sleep(pollingInterval) |
| 36 | continue |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return fmt.Errorf("Unexpected error waiting for the copy to complete") |
| 41 | } |