theriverman / django-minio-backend

Minio Backend for Django
https://pypi.org/project/django-minio-backend/
MIT License
111 stars 22 forks source link

Error: a bytes-like object is required, not 'str' #52

Closed MrArabboy closed 2 weeks ago

MrArabboy commented 1 month ago

I updated minio package from 7.2.6 to 7.2.8 , After file uploaded to FileField and go to obj change page in django admin , raise error a bytes-like object is required, not 'str' image

BinDruid commented 4 weeks ago

With latest release of minio-py (version 7.2.8) and according to AWS S3 specification object_name has to be a string.

Is there any consideration to encode name any more?

Simply passing object_name as string will fix the issue.

  try:
      u: str = client.presigned_get_object(
          bucket_name=self.bucket,
          object_name=name.encode('utf-8'),
          expires=get_setting("MINIO_URL_EXPIRY_HOURS", timedelta(days=7))  # Default is 7 days
      )
      return u
  except urllib3.exceptions.MaxRetryError:
      raise ConnectionError("Couldn't connect to Minio. Check django_minio_backend parameters in Django-Settings")
MrArabboy commented 3 weeks ago

With latest release of minio-py (version 7.2.8) and according to AWS S3 specification object_name has to be a string.

Is there any consideration to encode name any more?

Simply passing object_name as string will fix the issue.

  try:
      u: str = client.presigned_get_object(
          bucket_name=self.bucket,
          object_name=name.encode('utf-8'),
          expires=get_setting("MINIO_URL_EXPIRY_HOURS", timedelta(days=7))  # Default is 7 days
      )
      return u
  except urllib3.exceptions.MaxRetryError:
      raise ConnectionError("Couldn't connect to Minio. Check django_minio_backend parameters in Django-Settings")

I have model:

   class PersonDocument(CreatedUpdatedAbstractModel):
         person = models.ForeignKey(
             "employment.Person", on_delete=models.CASCADE, related_name="documents"
         )
         file = models.FileField(
            storage=MinioBackend(
               bucket_name=settings.DOCUMENT_PRIVATE_BUCKET,
            ),
            upload_to=get_person_document_upload_path,
           )

def get_person_document_upload_path(instance: Model, filename: str) -> str:
    return os.path.join(
        "person", str(instance.person.id), "documents", str(filename)
    ).replace("\\", "/")`
error cause might be get_person_document_upload_path ?
poulpreben commented 3 weeks ago

A workaround is suggested in #51.

However, I agree with @BinDruid that removing the encoding of object_name is the right approach since validation is handled further downstream by the minio-py package.

I've created a PR: https://github.com/theriverman/django-minio-backend/pull/53

theriverman commented 2 weeks ago

version 3.7.2 fixes this issue. please check, and let me know if you experience any other problems!