googleapis / googleapis/google-api-nodejs-client

drive_v3 files.create unable to get resumable session URI

Aperta
#3,426 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

api: drive priority: p3 type: feature request
Lingua principale
TypeScript
Stelle
12.3k
Fork
2k
Merge medio
1g 9h
PR unite (30g)
24

Descrizione

Feature

I think that would be nice to be able to get the resumable session URI by specifying the uploadType: 'resumable' parameters on the drive_v3 files.create method, instead of doing a POST request.

const params = {
  uploadType: 'resumable'
};

const options = {};

const res = await drive.files.create(params, options);
const uri = res.headers.location;
Motivation

Initially I thought that was the case by reading the documentation on how to perform a resumable upload (link).
So I take a look at the code to understand how to do that but unfortunately it seems impossible.

To summarise the documentation to get a resumable URI we need to do a POST like so:
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

If we take a look at how the create method it's implemented we found out that we have this URI in to the mediaUrl attribute. (link)

If we take a look how the request it's constructed we found out that only when we have the media.body the correct url it's used. But then the uploadType it's changed in to something else. So the POST request can't be constructed as desired. (link)

Solution

In this case a first simple fix would be modify the createAPIRequestAsync of googleapis-common to add another if statement to enforce the right URI when we have an empty body and the uploadType is set to "resumable". Pretty much like so:

if (parameters.mediaUrl && media.body) {
  options.url = parameters.mediaUrl;
  if (resource) {
    params.uploadType = 'multipart';
    // more code
  } else {
    params.uploadType = 'media';
    // more code
  }
} else if (parameters.mediaUrl && params.uploadType === 'resumable') {
  options.url = parameters.mediaUrl;
} else {
  options.data = resource || undefined;
}

The current solution that I could find instead is to override the URI in the options like so:

 const options = {
  rootUrl: 'https://www.googleapis.com/upload'
};

Please let me know if i'm missing something or if I should proceed with a pull request on nodejs-googleapis-common repository.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla definizione di drive_v3 files.create in src/apis/drive/v3.ts, quindi esamina createAPIRequestAsync nel src/apirequest.ts collegato di nodejs-googleapis-common. Confronta la costruzione della richiesta con la documentazione di Drive sui resumable uploads; il lavoro è completato quando files.create può ottenere l’URI della sessione resumable tramite l’uploadType specificato senza richiedere un POST manuale o un override di rootUrl.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
nodejs, typescript
Ambito
api
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.