แก้ไข

แชร์ผ่าน


Delete and restore a blob with Go

This article shows how to delete blobs using the Azure Storage client module for Go, and how to restore soft-deleted blobs during the retention period.

Prerequisites

Set up your environment

If you don't have an existing project, this section shows how to set up a project to work with the Azure Blob Storage client module for Go. The steps include module installation, adding import paths, and creating an authorized client object. For details, see Get started with Azure Blob Storage and Go.

Install modules

Install the azblob module using the following command:

go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblob

To authenticate with Microsoft Entra ID (recommended), install the azidentity module using the following command:

go get github.com/Azure/azure-sdk-for-go/sdk/azidentity

Add import paths

In your code file, add the following import paths:

import (
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)

These import paths represent the minimum needed to get started. Some code examples in this article might require additional import paths. For specific details and example usage, see Code samples.

Create a client object

To connect an app to Blob Storage, create a client object using azblob.NewClient. The following example shows how to create a client object using DefaultAzureCredential for authorization:

func getServiceClientTokenCredential(accountURL string) *azblob.Client {
    // Create a new service client with token credential
    credential, err := azidentity.NewDefaultAzureCredential(nil)
    handleError(err)

    client, err := azblob.NewClient(accountURL, credential, nil)
    handleError(err)

    return client
}

Authorization

The authorization mechanism must have the necessary permissions to delete a blob, or to restore a soft-deleted blob. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Delete Blob (REST API) and Undelete Blob (REST API).

Delete a blob

Note

When blob soft delete is enabled for a storage account, you can't perform a permanent deletion using client library methods. Using the methods in this article, a soft-deleted blob, blob version, or snapshot remains available until the retention period expires, at which time it's permanently deleted. To learn more about the underlying REST API operation, see Delete Blob (REST API).

To delete a blob, call the following method:

The following example deletes a blob:

func deleteBlob(client *azblob.Client, containerName string, blobName string) {
    // Delete the blob
    _, err := client.DeleteBlob(context.TODO(), containerName, blobName, nil)
    handleError(err)
}

If the blob has any associated snapshots, you must delete all of its snapshots to delete the blob. The following example deletes a blob and its snapshots:

func deleteBlobWithSnapshots(client *azblob.Client, containerName string, blobName string) {
    // Delete the blob and its snapshots
    _, err := client.DeleteBlob(context.TODO(), containerName, blobName, &blob.DeleteOptions{
        DeleteSnapshots: to.Ptr(blob.DeleteSnapshotsOptionTypeInclude),
    })
    handleError(err)
}

To delete only the snapshots and not the blob itself, you can pass the value DeleteSnapshotsOptionTypeOnly to the DeleteSnapshots parameter.

Restore a deleted blob

Blob soft delete protects an individual blob and its versions, snapshots, and metadata from accidental deletes or overwrites by maintaining the deleted data in the system for a specified period of time. During the retention period, you can restore the blob to its state at deletion. After the retention period expires, the blob is permanently deleted. For more information about blob soft delete, see Soft delete for blobs.

You can use the Azure Storage client libraries to restore a soft-deleted blob or snapshot.

How you restore a soft-deleted blob depends on whether or not your storage account has blob versioning enabled. For more information on blob versioning, see Blob versioning. See one of the following sections, depending on your scenario:

Restore soft-deleted objects when versioning is disabled

To restore deleted blobs when versioning is disabled, call the following method:

This method restores the content and metadata of a soft-deleted blob and any associated soft-deleted snapshots. Calling this method for a blob that hasn't been deleted has no effect.

func restoreDeletedBlob(client *azblob.Client, containerName string, blobName string) {
    // Reference the blob as a client object
    blobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    // Restore the deleted blob
    _, err := blobClient.Undelete(context.TODO(), &blob.UndeleteOptions{})
    handleError(err)
}

Restore soft-deleted objects when versioning is enabled

If a storage account is configured to enable blob versioning, deleting a blob causes the current version of the blob to become the previous version. To restore a soft-deleted blob when versioning is enabled, copy a previous version over the base blob. You can use the following method:

The following code example identifies a version of a deleted blob, and restores that version by copying it to the base blob:

func restoreDeletedBlobVersion(client *azblob.Client, containerName string, blobName string, versionID string) {
    // Reference the blob as a client object
    baseBlobClient := client.ServiceClient().NewContainerClient(containerName).NewBlobClient(blobName)

    blobVersionClient, err := baseBlobClient.WithVersionID(versionID)
    handleError(err)

    // Restore the blob version by copying it to the base blob
    _, err = baseBlobClient.StartCopyFromURL(context.TODO(), blobVersionClient.URL(), nil)
    handleError(err)
}

Note

The code samples in this guide are intended to help you get started with Azure Blob Storage and Go. You should modify error handling and Context values to meet the needs of your application.

Resources

To learn more about how to delete blobs and restore deleted blobs using the Azure Blob Storage client module for Go, see the following resources.

Code samples

REST API operations

The Azure SDK for Go contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Go paradigms. The client library methods for deleting blobs and restoring deleted blobs use the following REST API operations:

Client module resources

See also

  • This article is part of the Blob Storage developer guide for Go. To learn more, see the full list of developer guide articles at Build your Go app.