Skip to content

Commit a283f2b

Browse files
authored
feat(azure_blob): preserve content type and md5 (#2734)
1 parent bb3bc31 commit a283f2b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

drivers/azure_blob/driver.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package azure_blob
22

33
import (
44
"context"
5+
"encoding/hex"
56
"fmt"
67
"io"
78
"path"
@@ -12,10 +13,13 @@ import (
1213
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
1314
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1415
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
16+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
1517
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
1618
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
1719
"github.com/OpenListTeam/OpenList/v4/internal/driver"
1820
"github.com/OpenListTeam/OpenList/v4/internal/model"
21+
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
22+
log "github.com/sirupsen/logrus"
1923
)
2024

2125
// Azure Blob Storage based on the blob APIs
@@ -283,6 +287,26 @@ func (d *AzureBlob) Put(ctx context.Context, dstDir model.Obj, stream model.File
283287
// Determine optimal upload options based on file size
284288
options := optimizedUploadOptions(stream.GetSize())
285289

290+
// Preserve the file content type for direct Azure Blob downloads.
291+
contentType := stream.GetMimetype()
292+
if contentType == "" {
293+
contentType = "application/octet-stream"
294+
}
295+
options.HTTPHeaders = &blob.HTTPHeaders{
296+
BlobContentType: &contentType,
297+
}
298+
299+
// Preserve the MD5 checksum when it is available.
300+
md5Hex := stream.GetHash().GetHash(utils.MD5)
301+
if md5Hex != "" {
302+
md5, err := hex.DecodeString(md5Hex)
303+
if err == nil && len(md5) == 16 {
304+
options.HTTPHeaders.BlobContentMD5 = md5
305+
} else {
306+
log.Warnf("Invalid MD5 hash: %s, error: %v", md5Hex, err)
307+
}
308+
}
309+
286310
// Track upload progress
287311
progressTracker := &progressTracker{
288312
total: stream.GetSize(),

0 commit comments

Comments
 (0)