Last active
July 19, 2021 00:33
-
-
Save csotiriou/83436faba05d76698f2e8684b47d66e6 to your computer and use it in GitHub Desktop.
Upload file provider for Strapi - OVH with OpenStack. Article https://oramind.com/develop-strapi-upload-provider/
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
const pkgcloud = require('pkgcloud'); | |
const streamifier = require('streamifier'); | |
module.exports = { | |
init(providerOptions) { | |
const client = pkgcloud.storage.createClient(providerOptions); | |
const options = { container: providerOptions.defaultContainerName } | |
const remoteURL = () => | |
new Promise((resolve, reject) => { | |
return client.getContainer(providerOptions.defaultContainerName, (err, res) => { | |
if (err && !res) return reject(err); | |
return resolve(res); | |
}); | |
}); | |
//returning an object with two methods defined | |
return { | |
upload(file) { | |
const readStream = streamifier.createReadStream(file.buffer); | |
const writeStream = client.upload({ | |
...options, | |
remote: file.hash, | |
contentType: file.mime, | |
}); | |
return new Promise((resolve, reject) => { | |
readStream.pipe(writeStream); | |
writeStream.on('error', error => error && reject(error)); | |
writeStream.on('success', result => { | |
remoteURL() | |
.then(data => { | |
resolve( | |
Object.assign(file, { | |
mime: result.contentType, | |
url: `${providerOptions.publicUrlPrefix}/${result.name}`, | |
}) | |
); | |
}) | |
.catch(err => console.error(err) && reject(err)); | |
}); | |
}); | |
}, | |
delete(file) { | |
return new Promise((resolve, reject) => { | |
client.removeFile(providerOptions.defaultContainerName, file.hash, error => { | |
if (error) return reject(error); | |
return resolve(); | |
}); | |
}); | |
} | |
}; | |
}, | |
}; |
Options inside
config/plugins.js
:module.exports = ({ env }) => { return { upload: { provider: 'ovh', providerOptions: { keystoneAuthVersion: 'v3', provider: 'openstack', // required username: env('STORAGE_USERNAME','user-id'), password: env('STORAGE_PASSWORD', '<<storage-password>>'), region: env('STORAGE_REGION', 'DE'), domainId: env('STORAGE_DOMAIN_ID', 'default'), domainName: env('STORAGE_TENANT_NAME','tenant_name'), authUrl: env('STORAGE_AUTH_URL', 'https://auth.cloud.ovh.net/'), defaultContainerName: env('STORAGE_CONTAINER_NAME', '<<storage container name>>'), publicUrlPrefix: env('STORAGE_PUBLIC_URL_PREFIX', 'your public files URL') }, }, } };```
@csotiriou Hiya. I'm trying to use your gist, but I'm getting the error 401 Unauthorized in console when trying uploading file.
I was double-checked every detail in plugins.js and I believe everything is correct there. Do you have any suggestions on how to deal with it?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Options inside
config/plugins.js
: