{"id":"194f6b08dab5d29e","repo":"boto/boto3","slug":"fileobj-must-implement-read","errorCode":null,"errorMessage":"Fileobj must implement read","messagePattern":"Fileobj must implement read","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"boto3/s3/inject.py","lineNumber":660,"sourceCode":"\n    :type Key: str\n    :param Key: The name of the key to upload to.\n\n    :type ExtraArgs: dict\n    :param ExtraArgs: Extra arguments that may be passed to the\n        client operation. For allowed upload arguments see\n        :py:attr:`boto3.s3.transfer.S3Transfer.ALLOWED_UPLOAD_ARGS`.\n\n    :type Callback: function\n    :param Callback: A method which takes a number of bytes transferred to\n        be periodically called during the upload.\n\n    :type Config: boto3.s3.transfer.TransferConfig\n    :param Config: The transfer configuration to be used when performing the\n        upload.\n    \"\"\"\n    if not hasattr(Fileobj, 'read'):\n        raise ValueError('Fileobj must implement read')\n\n    subscribers = None\n    if Callback is not None:\n        subscribers = [ProgressCallbackInvoker(Callback)]\n\n    config = Config\n    if config is None:\n        config = TransferConfig()\n\n    with create_transfer_manager(self, config) as manager:\n        future = manager.upload(\n            fileobj=Fileobj,\n            bucket=Bucket,\n            key=Key,\n            extra_args=ExtraArgs,\n            subscribers=subscribers,\n        )\n        return future.result()","sourceCodeStart":642,"sourceCodeEnd":678,"githubUrl":"https://github.com/boto/boto3/blob/c7b4afac237b976d48395d7523eaf7cec3a450b3/boto3/s3/inject.py#L642-L678","documentation":"Raised by the injected upload_fileobj() helper (on the S3 client, Bucket, and Object resources) when the Fileobj argument lacks a 'read' attribute. upload_fileobj streams bytes from a file-like object to S3 via the managed transfer manager, and it requires an object that implements read() returning bytes. Passing anything that is not a readable binary stream is rejected up front with this ValueError.","triggerScenarios":"Calling s3.upload_fileobj(Fileobj, Bucket, Key) / bucket.upload_fileobj(...) where Fileobj is a path string (meant for upload_file instead), a bytes object, a file opened in text mode ('r' without 'b'), a StringIO, None, or any object without a read method.","commonSituations":"Confusing upload_file (takes a filename string) with upload_fileobj (takes an open file); opening the file with open(path) (text mode) and forgetting 'rb'; passing the raw value returned from requests/urllib without wrapping it; passing a pathlib.Path object.","solutions":["Pass an already-opened binary file handle: with open(path, 'rb') as f: s3.upload_fileobj(f, bucket, key).","If you only have a filename/path, use s3.upload_file(filename, bucket, key) instead of upload_fileobj.","If you have an in-memory bytes payload, wrap it with io.BytesIO(data) before calling upload_fileobj.","Ensure you are not passing a text-mode handle or a string."],"exampleFix":"// before\ns3.upload_fileobj('data.txt', 'mybucket', 'key')  # wrong: string, not file-like\n\n// after\nwith open('data.txt', 'rb') as f:\n    s3.upload_fileobj(f, 'mybucket', 'key')","handlingStrategy":"type-guard","validationCode":"if not hasattr(fileobj, 'read'):\n    raise TypeError('upload_fileobj requires a readable file-like object')\ns3.upload_fileobj(fileobj, bucket, key)","typeGuard":"def is_readable_fileobj(obj) -> bool:\n    return hasattr(obj, 'read') and callable(getattr(obj, 'read'))","tryCatchPattern":"try:\n    s3.upload_fileobj(fileobj, bucket, key)\nexcept ValueError as e:\n    if 'must implement read' in str(e):\n        with open(path, 'rb') as f:\n            s3.upload_fileobj(f, bucket, key)","preventionTips":["Always open upload sources with open(path, 'rb').","Use upload_file(path, ...) when you only have a filename, and upload_fileobj(f, ...) when you have a handle.","Wrap in-memory payloads with io.BytesIO before uploading."],"tags":["boto3","s3","upload","fileobj","validation"],"analyzedSha":"c7b4afac237b976d48395d7523eaf7cec3a450b3","analyzedAt":"2026-08-04T20:35:51.598Z","schemaVersion":2}