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

mediafileupload has no attribute '_fd' #798

Closed
Stormwaker opened this issue Jan 14, 2020 · 6 comments · Fixed by #1127
Closed

mediafileupload has no attribute '_fd' #798

Stormwaker opened this issue Jan 14, 2020 · 6 comments · Fixed by #1127
Assignees
Labels
type: cleanup An internal cleanup or hygiene concern.

Comments

@Stormwaker
Copy link

Environment details

  • OS: Ubuntu 18.04
  • Python version: 3.7.4
  • pip version: 19.2.3
  • google-api-python-client version: current

Steps to reproduce

  1. Add simple file upload from example to quickstart example from https://developers.google.com/drive/api/v3/quickstart/python
  2. After running console shows error: attributeerror 'mediafileupload' object has no attribute '_fd'
@busunkim96
Copy link
Contributor

Hi @Stormwaker,

I'm unable to replicate the error. It looks like you are using this sample showing simple uploads.

Could you try again with the code below?

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive']

def main():
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)

    file_metadata = {'name': 'photo.jpg'}
    media = MediaFileUpload('files/photo.jpg',
                            mimetype='image/jpeg')
    file = service.files().create(body=file_metadata,
                                        media_body=media,
                                        fields='id').execute()
    print ('File ID: %s' % file.get('id'))

if __name__ == '__main__':
    main()

@busunkim96 busunkim96 added the type: question Request for information or clarification. Not an issue. label Jan 14, 2020
@Stormwaker
Copy link
Author

Stormwaker commented Jan 14, 2020

I've noticed that there was a typo in the path to photo I wanted to upload. So I think that the error might be coming from exception handling. Full error reads:

Exception ignored in: <function MediaFileUpload.__del__ at 0x7fe736c88d40>
Traceback (most recent call last):
  File "/home/maccutkiewicz/anaconda3/lib/python3.7/site-packages/googleapiclient/http.py", line 567, in __del__
    self._fd.close()
AttributeError: 'MediaFileUpload' object has no attribute '_fd'

@busunkim96
Copy link
Contributor

👍 I'm able to replicate it if I misspell the filename.

@busunkim96 busunkim96 added type: cleanup An internal cleanup or hygiene concern. and removed type: question Request for information or clarification. Not an issue. labels Jan 14, 2020
@NikhilTank35
Copy link

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/googleapiclient/http.py", line 578, in del
self._fd.close()
AttributeError: 'MediaFileUpload' object has no attribute '_fd'

I got the same error while executing MediaFileUpload() but It's fine to work in the windows machine. Anyone here can please help me. Thanks

@Adhira-Deogade
Copy link

When I use the right directory and file address, it doesn't give me this error. My directory path starts with /Users//<folder_name>/<file_name>

@itskarantyagi
Copy link

@NikhilTank35

media = MediaFileUpload('files/photo.jpg',
                            mimetype='image/jpeg')

Make sure that files/photo.jpeg is a valid path. I was getting the same error, realized that I have no file at files/photo.jpeg in my repo.

@parthea parthea self-assigned this Dec 7, 2020
gcf-merge-on-green bot pushed a commit that referenced this issue Dec 9, 2020
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
- [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/google-api-python-client/issues/new/choose) before writing your code!  That way we can discuss the change, evaluate designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)

Fixes #798 🦕
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: cleanup An internal cleanup or hygiene concern.
Projects
None yet
6 participants