Cara buat web storage penyimpanan menggunakan api Google drive dapat upload dan buat…
- User
Cara buat web storage penyimpanan menggunakan api Google drive dapat upload dan buat link file download dan menggunakan react.js deploy ke vercel
- Assistant
Cara buat web storage penyimpanan menggunakan api Google drive dapat upload dan buat link file download dan menggunakan react.js deploy ke vercel
Membuat aplikasi untuk penyimpanan file menggunakan Google Drive API, mengupload file, membuat link file download, serta menggunakan React.js dan deploy ke Vercel memerlukan beberapa langkah. Berikut adalah panduan umum yang dapat Anda ikuti:
Akun Google: Untuk menggunakan Google Drive API.
Google Cloud Console:
credentials.json yang disediakan.React.js: Pastikan React.js sudah diinstal.
Vercel: Untuk deploy aplikasi.
Aktifkan Google Drive API:
Konfigurasi OAuth Consent Screen:
.../auth/drive.file untuk akses ke file di Google Drive.Buat Credentials:
http://localhost:3000).Inisialisasi Proyek React:
npx create-react-app google-drive-storage
cd google-drive-storage
Pasang Dependensi: Pasang library untuk membuat permintaan ke API.
npm install axios react-google-login
Struktur Proyek Anda: Buat struktur file berikut:
src/
└── components/
├── GoogleAuth.js # Untuk autentikasi Google.
├── UploadFile.js # Komponen untuk upload file dan membuat link download.
└── App.js # Komponen utama.
GoogleAuth.js: Untuk autentikasi Google menggunakan OAuth2.
import React from 'react';
import { GoogleLogin, googleLogout } from 'react-google-login';
const CLIENT_ID = 'YOUR_GOOGLE_CLIENT_ID';
const GoogleAuth = ({ setToken }) => {
const handleLoginSuccess = (response) => {
console.log("Login Success: ", response);
const token = response.tokenObj.access_token;
setToken(token);
};
const handleFailure = (response) => {
console.error("Login Failed: ", response);
};
return (
<div>
<GoogleLogin
clientId={CLIENT_ID}
buttonText="Login with Google"
onSuccess={handleLoginSuccess}
onFailure={handleFailure}
cookiePolicy={'single_host_origin'}
scope="https://www.googleapis.com/auth/drive.file"
/>
</div>
);
};
export default GoogleAuth;
UploadFile.js: Untuk upload file dan membuat link download.
Build Aplikasi: Di terminal, jalankan:
npm run build
Deploy ke Vercel:
Saat deploy ke Vercel:
CLIENT_ID di environment variables.Google Drive API memiliki limit rate dan kuota. Pastikan Anda memonitor statistik di Google Cloud Console.
Kini Anda memiliki aplikasi React.js yang menggunakan Google Drive API untuk upload file dan menghasilkan link download, serta telah dideploy ke Vercel! 😊
import React, { useState } from 'react';
import axios from 'axios';
const UploadFile = ({ token }) => {
const [file, setFile] = useState(null);
const [downloadLink, setDownloadLink] = useState("");
const handleFileChange = (event) => {
setFile(event.target.files[0]);
};
const handleUpload = async () => {
if (!file) return;
const formData = new FormData();
formData.append("file", file);
try {
const response = await axios.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
{
name: file.name,
},
{
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "multipart/related",
},
}
);
// Set permission of file to "public"
const fileId = response.data.id;
await axios.post(
`https://www.googleapis.com/drive/v3/files/${fileId}/permissions`,
{
role: "reader",
type: "anyone",
},
{
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
}
);
setDownloadLink(`https://drive.google.com/uc?id=${fileId}`);
} catch (error) {
console.error("Error uploading file: ", error);
}
};
return (
<div>
<input type="file" onChange={handleFileChange} />
<button onClick={handleUpload}>Upload</button>
{downloadLink && (
<p>
Download Link: <a href={downloadLink}>{downloadLink}</a>
</p>
)}
</div>
);
};
export default UploadFile;
App.js: Komponen utama untuk menggabungkan semuanya.
import React, { useState } from 'react';
import GoogleAuth from './components/GoogleAuth';
import UploadFile from './components/UploadFile';
const App = () => {
const [token, setToken] = useState(null);
return (
<div>
{!token ? (
<GoogleAuth setToken={setToken} />
) : (
<UploadFile token={token} />
)}
</div>
);
};
export default App;