Skip to content

Instantly share code, notes, and snippets.

@oskar-gmerek
Forked from csotiriou/ovhcloud.file-provider.js
Last active July 18, 2021 10:01
Show Gist options
  • Save oskar-gmerek/8f71060d2316c9a07e30c44d88bb9e21 to your computer and use it in GitHub Desktop.
Save oskar-gmerek/8f71060d2316c9a07e30c44d88bb9e21 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/ | original gist: https://gist.github.com/csotiriou/83436faba05d76698f2e8684b47d66e6
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();
});
});
}
};
},
};
{
"name": "strapi-provider-openstack-ovh",
"version": "1.0.0",
"maintainers": [
"https://gist.github.com/csotiriou/83436faba05d76698f2e8684b47d66e6"
],
"description": "ovh upload provider for strapi",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Christos Sotiriou",
"license": "ISC",
"dependencies": {
"pkgcloud": "^2.2.0",
"streamifier": "^0.1.1"
}
}
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')
},
},
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment