FastAPI SqlAdmin S3Storage class doubles aws s3 bucket_name when uploading file

22 Views Asked by At

I've been working on this for a few days and any help would be appreciated.

I've managed to create a bucket on AWS S3 and I am trying to upload to the bucket using FastAPI and SqlAdmin. I am using FastAPI S3Storage class and here is my code:


class s3BucketStorage(S3Storage):
    AWS_ACCESS_KEY_ID = AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY = AWS_SECRET_ACCESS_KEY
    AWS_S3_BUCKET_NAME = AWS_S3_BUCKET_NAME
    AWS_S3_ENDPOINT_URL = AWS_S3_ENDPOINT_URL
    AWS_DEFAULT_ACL = "bucket-owner-full-control"
    AWS_S3_USE_SSL = True

poster_storage = s3BucketStorage()

class LessonTranslation(Base):
    __tablename__ = "lesson_translations"

    LessonId = Column(
        Integer, ForeignKey("lessons.Id"), primary_key=True, nullable=False
    )
    LanguageId = Column(
        String(2), ForeignKey("languages.LanguageId"), primary_key=True, nullable=False
    )
    PosterUrl = Column(FileType(storage=poster_storage))
    Title = Column(String(150))
    LessonDescription = Column(String(500), nullable=True)
    Lesson = relationship("Lesson")
    Language = relationship("Language")

    def __str__(self):
        return self.Title

Now, the problem I am having is this. When the file is uploaded to S3, S3Storage class creates a folder with bucket name inside the bucket and actually writes the correct url to the database. I wanted it to let's say upload to "assets/posters/" directory inside the bucket. It uploads the file but uploads it to "https://<bucket_name>.s3.amazonaws.com/assets/posters/<bucket_name>/some_image.jpg" It actually repeats the bucket_name and since there is no folder with the bucket_name it creates it.

It looks like something changed with aws and S3Storage class did not reflect the recent changes of amazon? May be?

So, I have 2 problems

  1. Doubling <bucket_name> in the path ( explained above )
  2. I cannot write to bucket if I don't use
AWS_DEFAULT_ACL = "bucket-owner-full-control"

option.

AWS_DEFAULT_ACL = "public-read"

fails.

Any help would be appreciated.

0

There are 0 best solutions below