Google has added an option for hosting sites directly from its Drive service. All you need to do is upload the right files and set them to public. Steps To Upload Files To Anyone’s Google Drive Account Step 1: This is the most important step in this method. Ask the person you want to share the files to create a new folder in his Google Drive account. A folder can be created by clicking on the New Button at the top left corner of the Google Drive Homepage and then selecting the folder.
The Drive API allows you to upload file data when youcreate orupdate aFile resource.
In this guide and reference, media refers to all available files with MIME types that we support for upload to Google Drive. For a list of some of the MIME types that begin with 'application/vnd.google-apps', see create. The user can upload any file they want to Drive and some MIME type will be set for the item. If the format of the content can't be detected, then the MIME type will be set to 'application/octet-stream'.
When you upload media, you use a special URI. Methods that supportmedia uploads have two URI endpoints:
Upload types
There are three types of uploads you can perform:
Most Google API client libraries implement at least one of the methods. Referto the client library documentation for additional details on how to useeach of the methods.
Perform a simple upload
A simple upload is the most straightforward way to upload a file.Use this option if:
If you need to provide metadata for the file, you can usea multipart upload or resumable upload instead.
For larger files (more than 5 MB) or less reliable networkconnections, use the resumable upload.
These examples show how to upload an image using the client libraries:
Send a simple upload request
To use simple upload:
Example: Send a simple upload request
This example shows a simple upload request:
If the request succeeds, the server returns the HTTP
200 OK status code alongwith the file's metadata:
A blob uploaded to Drive gets 'Untitled' as the default title.For information about how to handle errors, refer to Handle errors.
Perform a multipart upload
A multipart upload request allows you to send metadata along with the datato upload. Use this option if the data you send is small enough to uploadagain in its entirety if the connection fails.
If your file does not have any metadata, use simple uploadinstead. For larger files (more than 5 MB) or less reliable network connections,use resumable upload instead.
Send a multipart upload request
To use multipart upload:
POST or PUT request to the standard resourceendpoint: https://www.googleapis.com/drive/v2/files
Example: Send a multipart upload request
This example shows a multipart upload request:
If the request succeeds, the server returns the HTTP
200 OK status code alongwith the file's metadata:
To handle errors, refer to Handle errors.
Perform a resumable upload
This protocol allows you to resume an upload operation after acommunication failure interrupts the flow of data. Use this option if:
Resumable uploads can also reduce your bandwidth usage when there is a networkfailure, because you don't have to restart large file uploads from thestart.
If you send small files over a reliable network connection, you can usesimple upload or multipart upload instead.
Learn about request URIs
When you upload media, you use a special URI. In fact, methods that supportmedia uploads have two URI endpoints:
Initiate a resumable upload session
To initiate a resumable upload session:
Example: Initiate a resumable upload session
This example shows how to initiate a resumable session to uploada new file:
The next section describes how to handle the response.
Save the resumable session URI
If the session initiation request succeeds, the response includes a Note: A resumable session URI expires after one week.
200 OK HTTP status code. In addition, it includes a Location header that specifiesthe resumable session URI. Use the resumable session URI to upload the file dataand query the upload status.
Copy and save the resumable session URI so you can use it for subsequentrequests.
Example: Save the resumable session URI
This example shows a response that includes a resumable session URI:
Upload the file
There are two ways to upload a file with a resumable session:
Single request
To upload the file in a single request:
If the upload request is interrupted, or if you receive a
5xx response,follow the procedure in Resume an interrupted upload.
Multiple chunks
To upload the file in multiple chunks:
Example: Upload the fileSingle request
This example shows a resumable request to upload an entire2,000,000-byte JPEG file, and uses the resumable session URI obtained in theprevious step:
If the request succeeds, you receive a
200 OK or 201 Created response,along with any metadata associated with the resource.
Multiple chunks
This example shows a request that sends the first 524,288 bytes(512 KB) of the file, and uses the resumable session URI obtained in theprevious step:
If the request succeeds, the server responds with
308 Resume Incomplete ,along with a Range header that identifies the total number of bytes thathave been stored so far:
Use the upper value returned in the
Range header to determine where tostart the next chunk. Continue to PUT each chunk of the file until theentire file has been uploaded.
When the entire upload is complete, you receive a
200 OK or 201 Created response, along with any metadata associated with the resource.
Resume an interrupted upload
If an upload request is terminated before a response, or if youreceive a
503 Service Unavailable response, then you need to resume theinterrupted upload. To do this:
Example: Resume an interrupted upload
This example shows a request for the upload status:
The server's response uses the
Range header to indicate that it has receivedthe first 43 bytes of the file so far:
You can then send a request to resume the upload. Send the bytes that remain in the file. Start at byte 43:
Handle errors
When you upload media, be sure to follow these best practices related to handle errors:
For additional details, see Handling API Errors.
Import to Google Docs typesWhen you create a file in Google Drive, you can convert some types file into aGoogle Docs, Sheets or Slides document. Include theconvert query parameters and set the mimeType property of the file.This sample shows how to upload a CSV file as a spreadsheet:
The supported conversions are available dynamically in theAbout resource's
importFormats arrayand include:
When you upload and convert media during an
update request to a Google Doc,Sheet, or Slide the full contents of the document will be replaced.
When you convert images you can improve the quality of the OCR algorithm. Specify the applicable BCP 47 languagecode in the
ocrLanguage parameter. The extracted text will appear in the Google Docs documentalongside the embedded image.
Use a pregenerated ID to upload files
The Drive API allows you to retrieve a list of pregenerated file IDsthat can be used to upload and create resources. Upload and file creationrequests can then include these pregenerated IDs. Set the
id fieldsin the file metadata.
You can safely retry uploads with pregenerated IDs in the case of anindeterminate server error or timeout. If the file was successfullycreated, subsequent retries return a
HTTP 409 error, it does not create duplicate files.
Note: Pregenerated IDs are not supported for native Google Document creation, oruploads where conversion to native Google Document format is requested.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |