Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Support for make public #139

Open
ojhaujjwal opened this issue May 5, 2020 · 8 comments
Open

Support for make public #139

ojhaujjwal opened this issue May 5, 2020 · 8 comments

Comments

@ojhaujjwal
Copy link
Contributor

ojhaujjwal commented May 5, 2020

Is there a way we can support making individual files public?

Use-case: for public images.

@ojhaujjwal
Copy link
Contributor Author

It's won't be applicable for all the drivers like local; but gcp and s3 support it.

@ojhaujjwal
Copy link
Contributor Author

php's flysystem library provides a similar feature image

@RomainLanz
Copy link
Member

Hey @ojhaujjwal! 👋

We are happily accepting PR!

However, I'd recommend to wait until we finish to swap the architecture.
I'll let you know.

@ojhaujjwal
Copy link
Contributor Author

Thanks @RomainLanz. I will be happy to contribute after it's done.

@DealerUp
Copy link

DealerUp commented Dec 18, 2020

Hello everybody!

Here is my solution to this problem.

In my project I have the structure(AdonisJS V5):

/app/Services/Spaces
-- index.ts
-- AmazonWebServicesS3Storage.ts

File: index.ts

import { StorageManager } from '@slynova/flydrive'

import AmazonWebServicesS3Storage from './AmazonWebServicesS3Storage'

import config from '../../../config/storage'

const storage = new StorageManager(config)

storage.registerDriver('s3', AmazonWebServicesS3Storage)

export default storage

File: AmazonWebServicesS3Storage.ts

import { NoSuchBucket, FileNotFound, PermissionMissing, UnknownException } from '@slynova/flydrive'

import { AmazonWebServicesS3Storage } from '@slynova/flydrive-s3'

function handleError(err, path, bucket) {
  switch (err.name) {
    case 'NoSuchBucket':
      return new NoSuchBucket(err, bucket)
    case 'NoSuchKey':
      return new FileNotFound(err, path)
    case 'AllAccessDisabled':
      return new PermissionMissing(err, path)
    default:
      return new UnknownException(err, err.name, path)
  }
}

export default class MyAmazonWebServicesS3Storage extends AmazonWebServicesS3Storage {
  constructor(config) {
    super(config)
  }

  async putPublic(location: string, content: any) {
    const params = { Key: location, Body: content, Bucket: this.$bucket, ACL: 'public-read' }
    try {
      const result = await this.$driver.upload(params).promise()
      return { raw: result }
    } catch (e) {
      throw handleError(e, location, this.$bucket)
    }
  }
}

In a file in a distant world, the method is consumed like this:

import Route from '@ioc:Adonis/Core/Route'
import Spaces from 'App/Services/Spaces'

Route.post('spaces', async ({ request }) => {
  const file = request.file('name'),
    name = file.name

  return Spaces.disk('spaces').putPublic(name, file)
})

I extended the original AmazonWebServicesS3Storage class by adding the putPublic method with the 'public-read' ACL rule.

It is! MTF solution but functional!

🙃

@TrickSantos
Copy link

Hello everybody!

Here is my solution to this problem.

In my project I have the structure(AdonisJS V5):

/app/Services/Spaces
-- index.ts
-- AmazonWebServicesS3Storage.ts

File: index.ts

import { StorageManager } from '@slynova/flydrive'

import AmazonWebServicesS3Storage from './AmazonWebServicesS3Storage'

import config from '../../../config/storage'

const storage = new StorageManager(config)

storage.registerDriver('s3', AmazonWebServicesS3Storage)

export default storage

How is the configuration of your StorageManager?

@DealerUp
Copy link

Hello everybody!
Here is my solution to this problem.
In my project I have the structure(AdonisJS V5):
/app/Services/Spaces
-- index.ts
-- AmazonWebServicesS3Storage.ts
File: index.ts

import { StorageManager } from '@slynova/flydrive'

import AmazonWebServicesS3Storage from './AmazonWebServicesS3Storage'

import config from '../../../config/storage'

const storage = new StorageManager(config)

storage.registerDriver('s3', AmazonWebServicesS3Storage)

export default storage

How is the configuration of your StorageManager?

import Env from '@ioc:Adonis/Core/Env'
import path from 'path'

export const ROOT_PATH = path.resolve(__dirname, '../')
export const STORAGE_PATH = path.resolve(ROOT_PATH, 'resources/storage')
export const PUBLIC_PATH = path.resolve(ROOT_PATH, 'public')

export default {
  default: 'local',

  disks: {
    local: {
      driver: 'local',
      config: {
        root: STORAGE_PATH,
      },
    },

    public: {
      driver: 'local',
      config: {
        root: PUBLIC_PATH,
      },
    },

    spaces: {
      driver: 's3',
      config: {
        key: Env.get('DO_SPACES_KEY'),
        secret: Env.get('DO_SPACES_SECRET'),
        endpoint: Env.get('DO_SPACES_ENDPOINT'),
        bucket: Env.get('DO_SPACES_SPACE'),
        region: Env.get('DO_SPACES_REGION'),
      },
    },
  },
}

@njsubedi
Copy link

Sent a PR #166

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants