Skip to content

Instantly share code, notes, and snippets.

@csotiriou
Last active July 19, 2021 00:33
Show Gist options
  • Save csotiriou/83436faba05d76698f2e8684b47d66e6 to your computer and use it in GitHub Desktop.
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/
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();
});
});
}
};
},
};
@csotiriou
Copy link
Author

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')
      },
    },
  }
};```

@oskar-gmerek
Copy link

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