gcs.js 854 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { Storage } = require('@google-cloud/storage');
  2. const storage = new Storage();
  3. class GCSStorage {
  4. constructor(config, log) {
  5. this.bucket = storage.bucket(config.gcs_bucket);
  6. this.log = log;
  7. }
  8. async length(id) {
  9. const data = await this.bucket.file(id).getMetadata();
  10. return data[0].size;
  11. }
  12. getStream(id) {
  13. return this.bucket.file(id).createReadStream({ validation: false });
  14. }
  15. set(id, file) {
  16. return new Promise((resolve, reject) => {
  17. file
  18. .pipe(
  19. this.bucket.file(id).createWriteStream({
  20. validation: false,
  21. resumable: true
  22. })
  23. )
  24. .on('error', reject)
  25. .on('finish', resolve);
  26. });
  27. }
  28. del(id) {
  29. return this.bucket.file(id).delete();
  30. }
  31. ping() {
  32. return this.bucket.exists();
  33. }
  34. }
  35. module.exports = GCSStorage;