diff --git a/app/object-storage/object-storage.go b/app/object-storage/object-storage.go index b19c751241a08edd7f2d97df17c1e8735083be81..207b2cd3047426f804ac888eec6d03e094022ea5 100644 --- a/app/object-storage/object-storage.go +++ b/app/object-storage/object-storage.go @@ -28,6 +28,12 @@ func removeTransactionFromBlobStorage(transaction Transaction, database Database } func fakeTransaction(transactionToFake Transaction, fakeTransaction Transaction, database Database) Database { + for i, trans := range database { + if trans.Id == transactionToFake.Id { + database[i] = fakeTransaction + break + } + } return database } @@ -77,17 +83,6 @@ func writeDatabaseToBlobStorage(database Database, blob Blob) { } } -// Azure Storage Quickstart Sample - Demonstrate how to upload, list, download, and delete blobs. -// -// Documentation References: -// - What is a Storage Account - https://docs.microsoft.com/azure/storage/common/storage-create-storage-account -// - Blob Service Concepts - https://docs.microsoft.com/rest/api/storageservices/Blob-Service-Concepts -// - Blob Service Go SDK API - https://godoc.org/github.com/Azure/azure-storage-blob-go -// - Blob Service REST API - https://docs.microsoft.com/rest/api/storageservices/Blob-Service-REST-API -// - Scalability and performance targets - https://docs.microsoft.com/azure/storage/common/storage-scalability-targets -// - Azure Storage Performance and Scalability checklist https://docs.microsoft.com/azure/storage/common/storage-performance-checklist -// - Storage Emulator - https://docs.microsoft.com/azure/storage/common/storage-use-emulator - func randomString() string { r := rand.New(rand.NewSource(time.Now().UnixNano())) return strconv.Itoa(r.Int()) @@ -166,6 +161,18 @@ func ListBlobs(blob Blob) { } } +func PrintingDatabaseToConsole(database Database) { + for i, trans := range database { + fmt.Println("--------------------") + fmt.Printf("\nListing transactions number %d in database\n", i) + fmt.Printf("Transaction id: %s\n", trans.Id) + fmt.Printf("Transaction sender: %s\n", trans.Sender) + fmt.Printf("Transaction receiver: %s\n", trans.Receiver) + fmt.Printf("Transaction amount: %s\n", trans.Amount) + fmt.Println("--------------------") + } +} + func TestObjectStorage() { ctx := context.Background() // url := "https://hepiadistributedsystems.blob.core.windows.net/" //replace <StorageAccountName> with your Azure storage account name @@ -189,12 +196,33 @@ func TestObjectStorage() { db = addTransactionToBlobStorage(t1, db, objectStorage) db = addTransactionToBlobStorage(t2, db, objectStorage) - fmt.Printf("Press enter key to delete the blob fils, example container, and exit the application.\n") + fmt.Println("Listing files in container") + db1 := readDatabaseFromBlobStorage(objectStorage) + PrintingDatabaseToConsole(db1) + fmt.Println("Press enter to continue") + bufio.NewReader(os.Stdin).ReadBytes('\n') + + fmt.Println("Going to fake a transaction (replacing the first with a new one)") + t3 := Transaction{ + Id: "0", + Sender: "0", + Receiver: "1", + Amount: "1000000", + } + db = fakeTransaction(t1, t3, db) + writeDatabaseToBlobStorage(db, objectStorage) + fmt.Println("Listing files in container") + db2 := readDatabaseFromBlobStorage(objectStorage) + PrintingDatabaseToConsole(db2) + fmt.Println("Press enter to continue") + bufio.NewReader(os.Stdin).ReadBytes('\n') + + fmt.Println("Press enter key to delete the blob fils, example container, and exit the application.") bufio.NewReader(os.Stdin).ReadBytes('\n') - fmt.Printf("Cleaning up.\n") + fmt.Println("Cleaning up.") // Delete the blob - fmt.Printf("Deleting the blob " + blobName + "\n") + fmt.Println("Deleting the blob " + blobName) _, err := objectStorage.BlockBlobClient.Delete(ctx, nil) if err != nil { @@ -202,7 +230,7 @@ func TestObjectStorage() { } // Delete the container - fmt.Printf("Deleting the blob " + objectStorage.ContainerName + "\n") + fmt.Println("Deleting the blob " + objectStorage.ContainerName) _, err = objectStorage.ContainerClient.Delete(ctx, nil) if err != nil {