Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MrArabboy opened this issue Aug 20, 2024 · 4 comments
Closed

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

MrArabboy opened this issue Aug 20, 2024 · 4 comments

Comments

@MrArabboy
Copy link

MrArabboy commented Aug 20, 2024

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
Copy link

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
Copy link
Author

MrArabboy commented Aug 28, 2024

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
Copy link
Contributor

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: #53

theriverman added a commit that referenced this issue Sep 2, 2024
datetime import changed to fix the now obsolete .utcnow() call
typehint fix
pinned minio-py version to >=7.2.8
@theriverman
Copy link
Owner

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

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

No branches or pull requests

4 participants