-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
329 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
// If you want to use AWS as storage cloud | ||
NEXT_PUBLIC_AWS_ACCESS_KEY= | ||
NEXT_PUBLIC_AWS_SECRET_ACCESS_KEY= | ||
NEXT_PUBLIC_BUCKET_NAME= | ||
NEXT_PUBLIC_REGION= | ||
NEXT_PUBLIC_REGION= | ||
|
||
// If you want to use AWS as storage cloud | ||
NEXT_PUBLIC_AZURE_STORAGE_ACCOUNT_NAME= | ||
NEXT_PUBLIC_AZURE_STORAGE_CONTAINER= | ||
NEXT_PUBLIC_AZURE_SHARED_ACCESS_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { BlobServiceClient } from "@azure/storage-blob"; | ||
|
||
let instance = null; | ||
|
||
class AzureStorageClient { | ||
static #CONFIG = { | ||
storageAccountName: process.env.NEXT_PUBLIC_AZURE_STORAGE_ACCOUNT_NAME, | ||
containerName: process.env.NEXT_PUBLIC_AZURE_STORAGE_CONTAINER, | ||
sasToken: process.env.NEXT_PUBLIC_AZURE_SHARED_ACCESS_TOKEN, | ||
}; | ||
|
||
constructor() { | ||
if (instance) { | ||
throw new Error("You can only create one instance of this class!"); | ||
} | ||
const blobServiceClient = new BlobServiceClient( | ||
`https://${ | ||
AzureStorageClient.#CONFIG.storageAccountName | ||
}.blob.core.windows.net/?${AzureStorageClient.#CONFIG.sasToken}` | ||
); | ||
this.containerClient = blobServiceClient.getContainerClient( | ||
AzureStorageClient.#CONFIG.containerName | ||
); | ||
instance = this; | ||
} | ||
|
||
getFileUrl(fileName) { | ||
return `https://${ | ||
AzureStorageClient.#CONFIG.storageAccountName | ||
}.blob.core.windows.net/${ | ||
AzureStorageClient.#CONFIG.containerName | ||
}/${fileName}`; | ||
} | ||
|
||
upload(file, onProgressChange) { | ||
const fileName = file.name; | ||
const blockBlobClient = this.containerClient.getBlockBlobClient( | ||
fileName | ||
); | ||
|
||
return { | ||
uploadPromise: blockBlobClient.uploadData(file, { | ||
onProgress: onProgressChange, | ||
}), | ||
fileUrl: this.getFileUrl(fileName), | ||
}; | ||
} | ||
} | ||
|
||
const azureStorageClient = Object.freeze(new AzureStorageClient()); | ||
export default azureStorageClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import awsStorageClient from "./AWS_S3"; | ||
import azureStorageClient from "./Azure"; | ||
|
||
export const uploadToAzure = (file, onProgressChange) => { | ||
const fileSize = file.size; | ||
|
||
return azureStorageClient.upload(file, ({ loadedBytes }) => { | ||
onProgressChange(loadedBytes / fileSize); | ||
}); | ||
}; | ||
|
||
export const uploadToAWS = (file, onProgressChange) => { | ||
return awsStorageClient.upload(file, ({ loaded, total }) => { | ||
onProgressChange(loaded / total); | ||
}); | ||
}; |
Oops, something went wrong.