adonisjs / drive-s3

S3 driver for AdonisJS drive
MIT License
21 stars 9 forks source link

Support drive s3 in Adonis 6 Drive #26

Open monojson opened 5 months ago

monojson commented 5 months ago

I find that Adonis Drive is not supported in Adonis 6, I don't know about when is the plan to implement it.

There is an alternative that could be implemented by now, but it would be best if the official would support it.

Looking forward to your reply, thanks!

mohamadlounnas commented 5 months ago

any updates?

e-e commented 4 months ago

@monojson what is the alternative?

monojson commented 4 months ago

@monojson what is the alternative?

Develop a package implements the S3 API, but it's not compatible with Adonis Drive.

If it's not urgent, you can wait for the official, maybe it's already in development(https://github.com/adonisjs/drive/issues/53).

carlosaroca commented 3 months ago

Hey @monojson @e-e @mohamadlounnas, I create a Service to do this while the official package is ready.

npm i aws-sdk

` import fs from 'fs' import AWS from 'aws-sdk' import env from '#start/env' import UtilsService from './util_service.js'

AWS.config.update({ accessKeyId: env.get('S3_KEY'), secretAccessKey: env.get('S3_SECRET'), region: env.get('S3_REGION'), })

const s3 = new AWS.S3()

export default class FileService { // public static async uploadFile(file: any, route: string) { const filePath = file.tmpPath const fileContent = fs.readFileSync(filePath)

const unique = await UtilsService.generateString(6) // GENERATE RANDOM ID

const fileName = `${route}/${unique}_${file.clientName}`

const params: any = {
  Bucket: env.get('S3_BUCKET'),
  Key: `${route}/${fileName}`,
  Body: fileContent,
}

try {
  const data = await s3.upload(params).promise()
  console.log(`File uploaded successfully. URL: ${data.Location}`)

  return await this.replaceCdn(data.Location)
} catch (err) {
  console.error('Error uploading file:', err)
  throw err
}

}

public static async deleteFile(fileName: string, route: string) { const params: any = { Bucket: env.get('S3_BUCKET'), Key: ${route}/${fileName}, }

try {
  await s3.deleteObject(params).promise()
  console.log(`File deleted successfully: ${fileName}`)
} catch (err) {
  console.error('Error deleting file:', err)
  throw err
}

}

public static async replaceCdn(file: string) { const cdn = file.replace( 'AWS URL BASE', 'YOUR CDN' ) return cdn } } `