-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
FilesAdapter for Azure Blob Storage #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,122 @@ | ||
// AzureBlobStorageAdapter | ||
// | ||
// Stores Parse files in Azure Blob Storage. | ||
|
||
import * as azure from 'azure-storage'; | ||
import { FilesAdapter } from './FilesAdapter'; | ||
|
||
export class AzureBlobStorageAdapter extends FilesAdapter { | ||
// Creates an Azure Storage client. | ||
// Provide storage account name or storage account connection string as first parameter | ||
// Provide container name as second parameter | ||
// If you had provided storage account name, then also provide storage access key | ||
// Host is optional, Azure will default to the default host | ||
// directAccess defaults to false. If set to true, the file URL will be the actual blob URL | ||
constructor( | ||
storageAccountOrConnectionString, | ||
container, { | ||
storageAccessKey = '', | ||
host = '', | ||
directAccess = false | ||
} = {} | ||
) { | ||
super(); | ||
|
||
this._storageAccountOrConnectionString = storageAccountOrConnectionString; | ||
this._storageAccessKey = storageAccessKey; | ||
this._host = host; | ||
this._container = container; | ||
this._directAccess = directAccess; | ||
if (this._storageAccountOrConnectionString.indexOf(';') != -1) { | ||
// Connection string was passed | ||
// Extract storage account name | ||
// Storage account name is needed in getFileLocation | ||
this._storageAccountName = this._storageAccountOrConnectionString.substring( | ||
this._storageAccountOrConnectionString.indexOf('AccountName') + 12, | ||
this._storageAccountOrConnectionString.indexOf(';', this._storageAccountOrConnectionString.indexOf('AccountName') + 12) | ||
); | ||
} else { | ||
// Storage account name was passed | ||
this._storageAccountName = this._storageAccountOrConnectionString; | ||
} | ||
// Init client | ||
this._azureBlobStorageClient = azure.createBlobService(this._storageAccountOrConnectionString, this._storageAccessKey, this._host); | ||
} | ||
|
||
// For a given config object, filename, and data, store a file in Azure Blob Storage | ||
// Returns a promise containing the Azure Blob Storage blob creation response | ||
createFile(config, filename, data) { | ||
let containerParams = {}; | ||
if (this._directAccess) { | ||
containerParams.publicAccessLevel = 'blob'; | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
this._azureBlobStorageClient.createContainerIfNotExists( | ||
this._container, | ||
containerParams, | ||
(cerror, cresult, cresponse) => { | ||
if (cerror) { | ||
return reject(cerror); | ||
} | ||
this._azureBlobStorageClient.createBlockBlobFromText( | ||
this._container, | ||
filename, | ||
data, | ||
(error, result, response) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
resolve(result); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
deleteFile(config, filename) { | ||
return new Promise((resolve, reject) => { | ||
this._azureBlobStorageClient.deleteBlob( | ||
this._container, | ||
filename, | ||
(error, response) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
resolve(response); | ||
}); | ||
}); | ||
} | ||
|
||
// Search for and return a file if found by filename | ||
// Returns a promise that succeeds with the buffer result from Azure Blob Storage | ||
getFileData(config, filename) { | ||
return new Promise((resolve, reject) => { | ||
this._azureBlobStorageClient.getBlobToText( | ||
this._container, | ||
filename, | ||
(error, text, blob, response) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
if(Buffer.isBuffer(text)) { | ||
resolve(text); | ||
} | ||
else { | ||
resolve(new Buffer(text, 'utf-8')); | ||
} | ||
|
||
}); | ||
}); | ||
} | ||
|
||
// Generates and returns the location of a file stored in Azure Blob Storage for the given request and filename | ||
// The location is the direct Azure Blob Storage link if the option is set, otherwise we serve the file through parse-server | ||
getFileLocation(config, filename) { | ||
if (this._directAccess) { | ||
return `http://${this._storageAccountName}.blob.core.windows.net/${this._container}/${filename}`; | ||
} | ||
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename)); | ||
} | ||
} | ||
|
||
export default AzureBlobStorageAdapter; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be more consistent with other adapters to only have one parameter
options