0
votes

I'm creating an image file in drive v3 successfully, but cannot retrieve the URI for the file; I'm expecting to find it in the success response_object.

I do find the file ID in the response_object and I can use that to construct a URI that displays the image:

https://drive.google.com/file/d/0Bw4DMtLCtPMkNERiTnpVNDdQV2c/view

I would like to have 'an official/correct' URI to hand to the google Cloud Vision API as an image source. I think I'm looking for the webContentLink v3-file metadata. I guess, more generally, I can't see how to get all the metadata (as described on https://developers.google.com/drive/v3/reference/files) for the file that I have just created.

        var newFileMetadata = {
          'name': unique_file_name,
          description: options.multipart,
          useContentAsIndexableText: false,
          parents: [ file.id ]
        };

        var media = {
          mimeType: 'image/jpg',
          body: fs.createReadStream(options.src_dir + '/' + sourceFile),
          viewersCanCopyContent: true,
          writersCanShare: true
        };

        var request_object = drive.files.create({
            auth: auth,
            resource: newFileMetadata,
            media: media,
            fields: ['id']
            },
            function(err, response) {
              if (err) {
                console.log('drive.files.create error: %s %s', err, response);
                return;
              } else {
                // file create success; get response
                console.log('dump the response\n %s', JSON.stringify(response));
              }
            }
          );  // end of drive.files.create()

and the console output is (with cr for readability):

dump the request_object

{"uri": {"protocol":"https:", "slashes":true, "auth":null, "host":"www.googleapis.com", "port":null, "hostname":"www.googleapis.com", "hash":null, "search":"?fields=id&uploadType=multipart", "query":"fields=id&uploadType=multipart", "pathname":"/upload/drive/v3/files", "path":"/upload/drive/v3/files?fields=id&uploadType=multipart", "href":"https://www.googleapis.com/upload/drive/v3/files?fields=id&uploadType=multipart"}, "method":"POST", "headers":{"Authorization":"Bearer ya29.GlxfB18oVtG6A3j7cXzSq17-RSmj9-2BlQm4zHD5sPzKkfhRM1FxlxUHc9mxWaka1N2fBiTJun-SYLB8ewuc63XVbUo01q0bS2FiS6iJTq9O1h9FQWfoO5r8E6z_6Q", "User-Agent":"google-api-nodejs-client/0.10.0","host":"www.googleapis.com", "transfer-encoding":"chunked", "content-type":"multipart/related; boundary=d6cef3f5-2246-5654-ac1f-d00561be5e8a"} }

dump the response {"id":"0Bw4DMtLCtPMkNERiTnpVNDdQV2c"}

to recap:

  • how do I get (in Node.js) a google Cloud Vision appropriate URI for a newly created google Drive v3 image file?
  • how do I get (in Node.js) the metadata for a newly created google Drive v3 image file?

Many thanks.

new information: I have tried a separate drive.files.get()

                drive.files.get({
                  auth: auth,
                  fileId: response.id
                },
                function(err, response) {
                  if (err) {
                  console.log('drive.files.get error: %s %s', err, response);
                  return;
                  } else {
                  // file create success; get properties
                  console.log('get properties\n %s', JSON.stringify(response));
                  }
                });

and I do receive some metadata (but not the big picture), console output, new upload:

get properties {"kind":"drive#file","id":"0Bw4DMtLCtPMkWFdTMUVwY2tRZ2c","name":"2017-06-04T17:57:42.189Zlovelock.png","mimeType":"image/png"}

1

1 Answers

1
votes

Use Files.get and specify in the 'field' parameter that you want to get webContentLink. It will return just that. Use the Try-it from the docs.

The complete list of metadata you can obtain is found in Files resource.

For testing purposes, pass your fileId here and run Execute.