Skip to content

Commit 5ade526

Browse files
committed
support direct url for azure blob
1 parent e03829f commit 5ade526

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

custom/conf/app.example.ini

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ LEVEL = Info
18321832
;STORAGE_TYPE = local
18331833
;;
18341834
;; Allows the storage driver to redirect to authenticated URLs to serve files directly
1835-
;; Currently, only `minio` is supported.
1835+
;; Currently, only `minio` and `azureblob` is supported.
18361836
;SERVE_DIRECT = false
18371837
;;
18381838
;; Path for attachments. Defaults to `attachments`. Only available when STORAGE_TYPE is `local`
@@ -1865,10 +1865,21 @@ LEVEL = Info
18651865
;;
18661866
;; Minio checksum algorithm: default (for MinIO or AWS S3) or md5 (for Cloudflare or Backblaze)
18671867
;MINIO_CHECKSUM_ALGORITHM = default
1868+
;;
1869+
;; Azure Blob endpoint to connect only available when STORAGE_TYPE is `azureblob`
1870+
;AZURE_BLOB_ENDPOINT =
1871+
;;
1872+
;; Azure Blob account name to connect only available when STORAGE_TYPE is `azureblob`
1873+
;AZURE_BLOB_ACCOUNT_NAME =
1874+
;;
1875+
;; Azure Blob account key to connect only available when STORAGE_TYPE is `azureblob`
1876+
;AZURE_BLOB_ACCOUNT_KEY =
1877+
;;
1878+
;; Azure Blob container to store the attachments only available when STORAGE_TYPE is `azureblob`
1879+
;AZURE_BLOB_CONTAINER = gitea
1880+
;;
18681881
;; override the azure blob base path if storage type is azureblob
18691882
;AZURE_BLOB_BASE_PATH = attachments/
1870-
;; override the azure blob base path if storage type is azureblob
1871-
;AZURE_BLOB_CONTAINER = attachments/
18721883

18731884
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18741885
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -2433,6 +2444,9 @@ LEVEL = Info
24332444
;MINIO_BASE_PATH = packages/
24342445
;; override the azure blob base path if storage type is azureblob
24352446
;AZURE_BLOB_BASE_PATH = packages/
2447+
;; Allows the storage driver to redirect to authenticated URLs to serve files directly
2448+
;; Currently, only `minio` and `azureblob` is supported.
2449+
;SERVE_DIRECT = false
24362450
;;
24372451
;; Path for chunked uploads. Defaults to APP_DATA_PATH + `tmp/package-upload`
24382452
;CHUNKED_UPLOAD_PATH = tmp/package-upload
@@ -2529,8 +2543,13 @@ LEVEL = Info
25292543
;; Where your lfs files reside, default is data/lfs.
25302544
;PATH = data/lfs
25312545
;;
2546+
;; Allows the storage driver to redirect to authenticated URLs to serve files directly
2547+
;; Currently, only `minio` and `azureblob` is supported.
2548+
;SERVE_DIRECT = false
2549+
;;
25322550
;; override the minio base path if storage type is minio
25332551
;MINIO_BASE_PATH = lfs/
2552+
;;
25342553
;; override the azure blob base path if storage type is azureblob
25352554
;AZURE_BLOB_BASE_PATH = lfs/
25362555

@@ -2547,7 +2566,7 @@ LEVEL = Info
25472566
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25482567
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25492568
;; customize storage
2550-
;[storage.my_minio]
2569+
;[storage.minio]
25512570
;STORAGE_TYPE = minio
25522571
;;
25532572
;; Minio endpoint to connect only available when STORAGE_TYPE is `minio`
@@ -2572,19 +2591,19 @@ LEVEL = Info
25722591
;MINIO_INSECURE_SKIP_VERIFY = false
25732592

25742593
;[storage.azureblob]
2575-
;STORAGE_TYPE=azureblob
2594+
;STORAGE_TYPE = azureblob
25762595
;;
25772596
;; Azure Blob endpoint to connect only available when STORAGE_TYPE is `azureblob`
2578-
;AZUREBLOB_ENDPOINT = https://<account_name>.blob.core.windows.net/
2597+
;AZURE_BLOB_ENDPOINT =
25792598
;;
25802599
;; Azure Blob account name to connect only available when STORAGE_TYPE is `azureblob`
2581-
;AZUREBLOB_ACCOUNT_NAME =
2600+
;AZURE_BLOB_ACCOUNT_NAME =
25822601
;;
25832602
;; Azure Blob account key to connect only available when STORAGE_TYPE is `azureblob`
2584-
;AZUREBLOB_ACCOUNT_KEY =
2603+
;AZURE_BLOB_ACCOUNT_KEY =
25852604
;;
25862605
;; Azure Blob container to store the attachments only available when STORAGE_TYPE is `azureblob`
2587-
;AZUREBLOB_CONTAINER =
2606+
;AZURE_BLOB_CONTAINER = gitea
25882607

25892608
;[proxy]
25902609
;; Enable the proxy, all requests to external via HTTP will be affected

modules/setting/storage.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ type MinioStorageConfig struct {
5454

5555
// MinioStorageConfig represents the configuration for a minio storage
5656
type AzureBlobStorageConfig struct {
57-
Endpoint string `ini:"AZUREBLOB_ENDPOINT" json:",omitempty"`
58-
AccountName string `ini:"AZUREBLOB_ACCOUNT_NAME" json:",omitempty"`
59-
AccountKey string `ini:"AZUREBLOB_ACCOUNT_KEY" json:",omitempty"`
60-
Container string `ini:"AZUREBLOB_CONTAINER" json:",omitempty"`
61-
BasePath string `ini:"AZUREBLOB_BASE_PATH" json:",omitempty"`
57+
Endpoint string `ini:"AZURE_BLOB_ENDPOINT" json:",omitempty"`
58+
AccountName string `ini:"AZURE_BLOB_ACCOUNT_NAME" json:",omitempty"`
59+
AccountKey string `ini:"AZURE_BLOB_ACCOUNT_KEY" json:",omitempty"`
60+
Container string `ini:"AZURE_BLOB_CONTAINER" json:",omitempty"`
61+
BasePath string `ini:"AZURE_BLOB_BASE_PATH" json:",omitempty"`
62+
ServeDirect bool `ini:"SERVE_DIRECT"`
6263
}
6364

6465
// Storage represents configuration of storages
@@ -95,6 +96,10 @@ func getDefaultStorageSection(rootCfg ConfigProvider) ConfigSection {
9596
storageSec.Key("MINIO_USE_SSL").MustBool(false)
9697
storageSec.Key("MINIO_INSECURE_SKIP_VERIFY").MustBool(false)
9798
storageSec.Key("MINIO_CHECKSUM_ALGORITHM").MustString("default")
99+
storageSec.Key("AZURE_BLOB_ENDPOINT").MustString("")
100+
storageSec.Key("AZURE_BLOB_ACCOUNT_NAME").MustString("")
101+
storageSec.Key("AZURE_BLOB_ACCOUNT_KEY").MustString("")
102+
storageSec.Key("AZURE_BLOB_CONTAINER").MustString("gitea")
98103
return storageSec
99104
}
100105

@@ -307,8 +312,9 @@ func getStorageForAzureBlob(targetSec, overrideSec ConfigSection, tp targetSecTy
307312
}
308313

309314
if overrideSec != nil {
310-
storage.AzureBlobConfig.BasePath = ConfigSectionKeyString(overrideSec, "AZUREBLOB_BASE_PATH", storage.AzureBlobConfig.BasePath)
311-
storage.AzureBlobConfig.Container = ConfigSectionKeyString(overrideSec, "AZUREBLOB_CONTAINER", storage.AzureBlobConfig.Container)
315+
storage.AzureBlobConfig.ServeDirect = ConfigSectionKeyBool(overrideSec, "SERVE_DIRECT", storage.MinioConfig.ServeDirect)
316+
storage.AzureBlobConfig.BasePath = ConfigSectionKeyString(overrideSec, "AZURE_BLOB_BASE_PATH", storage.AzureBlobConfig.BasePath)
317+
storage.AzureBlobConfig.Container = ConfigSectionKeyString(overrideSec, "AZURE_BLOB_CONTAINER", storage.AzureBlobConfig.Container)
312318
} else {
313319
storage.AzureBlobConfig.BasePath = defaultPath
314320
}

modules/storage/azureblob.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
2626
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
2727
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
28+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
2829
)
2930

3031
var _ ObjectStorage = &AzureBlobStorage{}
@@ -255,8 +256,15 @@ func (a *AzureBlobStorage) Delete(path string) error {
255256

256257
// URL gets the redirect URL to a file. The presigned link is valid for 5 minutes.
257258
func (a *AzureBlobStorage) URL(path, name string) (*url.URL, error) {
258-
// TODO: Support download blobs directly from azure
259-
return nil, ErrURLNotSupported
259+
blobClient := a.client.ServiceClient().NewContainerClient(a.container).NewBlobClient(a.buildAzureBlobPath(path))
260+
perm := sas.BlobPermissions{
261+
Read: true,
262+
}
263+
u, err := blobClient.GetSASURL(perm, time.Now().Add(5*time.Minute), &blob.GetSASURLOptions{})
264+
if err != nil {
265+
return nil, err
266+
}
267+
return url.Parse(u)
260268
}
261269

262270
// IterateObjects iterates across the objects in the azureblobstorage

routers/web/repo/download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob, lastModified *time.Tim
5353
return nil
5454
}
5555

56-
if setting.LFS.Storage.MinioConfig.ServeDirect {
57-
// If we have a signed url (S3, object storage), redirect to this directly.
56+
if (setting.LFS.Storage.MinioConfig.ServeDirect && setting.LFS.Storage.Type == setting.MinioStorageType) ||
57+
(setting.LFS.Storage.AzureBlobConfig.ServeDirect && setting.LFS.Storage.Type == setting.AzureBlobStorageType) {
58+
// If we have a signed url (S3, object storage, blob storage), redirect to this directly.
5859
u, err := storage.LFS.URL(pointer.RelativePath(), blob.Name())
5960
if u != nil && err == nil {
6061
ctx.Redirect(u.String())

0 commit comments

Comments
 (0)