mirror of
https://github.com/gilbertchen/duplicacy
synced 2025-12-15 07:43:21 +00:00
Fixed Azure storage after updating gilbertchen/azure-sdk-for-g
This commit is contained in:
@@ -14,14 +14,13 @@ import (
|
|||||||
type AzureStorage struct {
|
type AzureStorage struct {
|
||||||
RateLimitedStorage
|
RateLimitedStorage
|
||||||
|
|
||||||
clients []*storage.BlobStorageClient
|
containers []*storage.Container
|
||||||
container string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAzureStorage(accountName string, accountKey string,
|
func CreateAzureStorage(accountName string, accountKey string,
|
||||||
container string, threads int) (azureStorage *AzureStorage, err error) {
|
containerName string, threads int) (azureStorage *AzureStorage, err error) {
|
||||||
|
|
||||||
var clients []*storage.BlobStorageClient
|
var containers []*storage.Container
|
||||||
for i := 0; i < threads; i++ {
|
for i := 0; i < threads; i++ {
|
||||||
|
|
||||||
client, err := storage.NewBasicClient(accountName, accountKey)
|
client, err := storage.NewBasicClient(accountName, accountKey)
|
||||||
@@ -31,21 +30,21 @@ func CreateAzureStorage(accountName string, accountKey string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
blobService := client.GetBlobService()
|
blobService := client.GetBlobService()
|
||||||
clients = append(clients, &blobService)
|
container := blobService.GetContainerReference(containerName)
|
||||||
|
containers = append(containers, container)
|
||||||
}
|
}
|
||||||
|
|
||||||
exist, err := clients[0].ContainerExists(container)
|
exist, err := containers[0].Exists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exist {
|
if !exist {
|
||||||
return nil, fmt.Errorf("container %s does not exist", container)
|
return nil, fmt.Errorf("container %s does not exist", containerName)
|
||||||
}
|
}
|
||||||
|
|
||||||
azureStorage = &AzureStorage {
|
azureStorage = &AzureStorage {
|
||||||
clients: clients,
|
containers: containers,
|
||||||
container: container,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -77,7 +76,7 @@ func (azureStorage *AzureStorage) ListFiles(threadIndex int, dir string) (files
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
results, err := azureStorage.clients[threadIndex].ListBlobs(azureStorage.container, parameters)
|
results, err := azureStorage.containers[threadIndex].ListBlobs(parameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -115,14 +114,15 @@ func (azureStorage *AzureStorage) ListFiles(threadIndex int, dir string) (files
|
|||||||
|
|
||||||
// DeleteFile deletes the file or directory at 'filePath'.
|
// DeleteFile deletes the file or directory at 'filePath'.
|
||||||
func (storage *AzureStorage) DeleteFile(threadIndex int, filePath string) (err error) {
|
func (storage *AzureStorage) DeleteFile(threadIndex int, filePath string) (err error) {
|
||||||
_, err = storage.clients[threadIndex].DeleteBlobIfExists(storage.container, filePath)
|
_, err = storage.containers[threadIndex].GetBlobReference(filePath).DeleteIfExists(nil)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// MoveFile renames the file.
|
// MoveFile renames the file.
|
||||||
func (storage *AzureStorage) MoveFile(threadIndex int, from string, to string) (err error) {
|
func (storage *AzureStorage) MoveFile(threadIndex int, from string, to string) (err error) {
|
||||||
source := storage.clients[threadIndex].GetBlobURL(storage.container, from)
|
source := storage.containers[threadIndex].GetBlobReference(from)
|
||||||
err = storage.clients[threadIndex].CopyBlob(storage.container, to, source)
|
destination := storage.containers[threadIndex].GetBlobReference(to)
|
||||||
|
err = destination.Copy(source.GetURL(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,8 @@ func (storage *AzureStorage) CreateDirectory(threadIndex int, dir string) (err e
|
|||||||
|
|
||||||
// GetFileInfo returns the information about the file or directory at 'filePath'.
|
// GetFileInfo returns the information about the file or directory at 'filePath'.
|
||||||
func (storage *AzureStorage) GetFileInfo(threadIndex int, filePath string) (exist bool, isDir bool, size int64, err error) {
|
func (storage *AzureStorage) GetFileInfo(threadIndex int, filePath string) (exist bool, isDir bool, size int64, err error) {
|
||||||
properties, err := storage.clients[threadIndex].GetBlobProperties(storage.container, filePath)
|
blob := storage.containers[threadIndex].GetBlobReference(filePath)
|
||||||
|
err = blob.GetProperties(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Contains(err.Error(), "404") {
|
if strings.Contains(err.Error(), "404") {
|
||||||
return false, false, 0, nil
|
return false, false, 0, nil
|
||||||
@@ -145,7 +146,7 @@ func (storage *AzureStorage) GetFileInfo(threadIndex int, filePath string) (exis
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, false, properties.ContentLength, nil
|
return true, false, blob.Properties.ContentLength, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindChunk finds the chunk with the specified id. If 'isFossil' is true, it will search for chunk files with
|
// FindChunk finds the chunk with the specified id. If 'isFossil' is true, it will search for chunk files with
|
||||||
@@ -167,21 +168,22 @@ func (storage *AzureStorage) FindChunk(threadIndex int, chunkID string, isFossil
|
|||||||
|
|
||||||
// DownloadFile reads the file at 'filePath' into the chunk.
|
// DownloadFile reads the file at 'filePath' into the chunk.
|
||||||
func (storage *AzureStorage) DownloadFile(threadIndex int, filePath string, chunk *Chunk) (err error) {
|
func (storage *AzureStorage) DownloadFile(threadIndex int, filePath string, chunk *Chunk) (err error) {
|
||||||
readCloser, err := storage.clients[threadIndex].GetBlob(storage.container, filePath)
|
readCloser, err := storage.containers[threadIndex].GetBlobReference(filePath).Get(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer readCloser.Close()
|
defer readCloser.Close()
|
||||||
|
|
||||||
_, err = RateLimitedCopy(chunk, readCloser, storage.DownloadRateLimit / len(storage.clients))
|
_, err = RateLimitedCopy(chunk, readCloser, storage.DownloadRateLimit / len(storage.containers))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadFile writes 'content' to the file at 'filePath'.
|
// UploadFile writes 'content' to the file at 'filePath'.
|
||||||
func (storage *AzureStorage) UploadFile(threadIndex int, filePath string, content []byte) (err error) {
|
func (storage *AzureStorage) UploadFile(threadIndex int, filePath string, content []byte) (err error) {
|
||||||
reader := CreateRateLimitedReader(content, storage.UploadRateLimit / len(storage.clients))
|
reader := CreateRateLimitedReader(content, storage.UploadRateLimit / len(storage.containers))
|
||||||
return storage.clients[threadIndex].CreateBlockBlobFromReader(storage.container, filePath, uint64(len(content)), reader, nil)
|
blob := storage.containers[threadIndex].GetBlobReference(filePath)
|
||||||
|
return blob.CreateBlockBlobFromReader(reader, nil)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user