FireUser
FireUser

Reputation: 183

Create folder in specific folder GOOGLE DRIVE API V3

I am trying to get the names of the folders in my google drive programmatically and create a folder inside folder but there is no way.

I have reviewed this documentation:

Google Drive API EXAMPLE!

But how do I get a specific folder and create another one in it?.Programmatically, Not with an assistant.

Ok. I need update bitmap to google drive. Im trying with this code but doesnt working

        String IdFolder = (String) objects[0];
            Bitmap bitmapUpload = (Bitmap) objects[1];

            File fileMetadata = new File();
            fileMetadata.setTitle("photo.jpg");
            fileMetadata.setParents(Collections.singletonList(new ParentReference().setId(IdFolder)));

            File file = mService.files().insert(fileMetadata, bitmapUpload)
                    .setFields("id, parents")
                    .execute();
            System.out.println("File ID: " + file.getId());

I have tried this:

            String IdFolder = (String) objects[0];
            Bitmap bitmapUpload = (Bitmap) objects[1];

            int byteSize = bitmapUpload.getRowBytes() * bitmapUpload.getHeight();
            ByteBuffer byteBuffer = ByteBuffer.allocate(byteSize);
            bitmap.copyPixelsToBuffer(byteBuffer);


            byte[] byteArray = byteBuffer.array();


            ByteArrayInputStream bs = new ByteArrayInputStream(byteArray);

            File fileMetadata = new File();
            fileMetadata.setTitle("photo.jpg");
            fileMetadata.setParents(Collections.singletonList(new ParentReference().setId(IdFolder)));

            File file = mService.files().insert(fileMetadata, bs)
                    .setFields("id, parents")
                    .execute();
            System.out.println("File ID: " + file.getId());

But doesnt work. Just said:

com.google.api.client.http.abstract inputstreamcontent

?¿?

Upvotes: 2

Views: 23781

Answers (3)

Prem Danej
Prem Danej

Reputation: 219

The same problem I had faced, but after reading the documentation, I found this -> Create a folder

First, I create a Folder using API. In the response, I get the "Id" of the folder.

Endpoint -

POST https://www.googleapis.com/drive/v3/files

My Json Request -

{
  "name": "This is parents",
  "mimeType": "application/vnd.google-apps.folder"
}

Json Response

{
    "kind": "drive#file",
    "id": "1PcvFIWktxN21uO-20jTur6mpB790V8gI",
    "name": "This is parents",
    "mimeType": "application/vnd.google-apps.folder"
}

My folder has been created. In the JSON, I recieved "id". Just copy that.

Note - Using same API for creating a sub folder.

After that, I have to create a subfolder in it. So I Changed the JSON. Add "parents" key and paste that "id" like this.

My Json Request

{
  "name": "This is child",
  "parents": [
      "1PcvFIWktxN21uO-20jTur6mpB790V8gI" // pass that id
  ],
  "mimeType": "application/vnd.google-apps.folder"
}

Json Response -

{
    "kind": "drive#file",
    "id": "116Us0SdEfjJYgau0CeB2RVmcVOUSf9_o",
    "name": "This is child",
    "mimeType": "application/vnd.google-apps.folder"
}

Boom!.. Subfolder has been created.

Happy Coding :)

Upvotes: 3

Abhyam Gupta
Abhyam Gupta

Reputation: 175

I have tried to make a python function, you can use it in the way you want.

def create_folder_in_folder(folder_name,parent_folder_id):
    
    file_metadata = {
    'name' : folder_name,
    'parents' : [folder_id],
    'mimeType' : 'application/vnd.google-apps.folder'
    }

    file = drive_service.files().create(body=file_metadata,
                                    fields='id').execute()
    
    print ('Folder ID: %s' % file.get('id'))

EDIT suggested by@Jeremy Caney

so this function takes input of parent id of your particular folder which you want to keep as parent in which you want to create another folder. And other is the child folder name.

now, here

file_metadata = {
    'name' : folder_name,
    'parents' : [folder_id],
    'mimeType' : 'application/vnd.google-apps.folder'
    }

this parent id as a parent argument and in MIME type you are telling google server to create a file of type folder in the parent id parents, where name parameter describes the name of folder. You can see the MIME types provided by google here for your future codes references

now

file = drive_service.files().create(body=file_metadata,
                                    fields='id').execute()

this line just sends the request to the server to perform the action, where the file object contains the response of JSON format.

Here is the tool I made to do all necessary option using the terminal itself, that you usually do on google drive's website, you can read this as a reference.

hope this may help.

Upvotes: 9

MαπμQμαπkγVπ.0
MαπμQμαπkγVπ.0

Reputation: 6729

First thing to do is to make sure that your credentials are valid. To make requests to the drive API, follow the instruction specified in the quickstart of the documentation(assuming you will be using android). Then you can work with folders base on the documentation. Also take note that the parents property can be used when creating a folder as well to create a subfolder. Hope this helps.

Upvotes: 3

Related Questions