You've already forked flutter-rp-example
First commit
This commit is contained in:
44
lib/services/gcs.dart
Normal file
44
lib/services/gcs.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
|
||||
class GcsService {
|
||||
// ========================================
|
||||
// ======== GCS GOOGLE API SERVICE ========
|
||||
|
||||
static const GCS_GOOGLE_BASE_URI = "https://storage.googleapis.com";
|
||||
static const GCS_GOOGLE_UPLOAD_ENDPOINT = "/upload/storage/v1/b/";
|
||||
static const GET_BUCKET_STS_GOOGLE_ENDPOINT =
|
||||
"/api.GcsApi/GetBucketSTSGoogle";
|
||||
|
||||
Future<bool> uploadImage(String token, String bucketName, String resourceName,
|
||||
String fileName, File file) async {
|
||||
var headersData = {
|
||||
"Authorization": "Bearer $token",
|
||||
"Content-Type": "image/png",
|
||||
};
|
||||
var paramsData = {
|
||||
'uploadType': 'multipart/related; boundary=image/png',
|
||||
'name': "$resourceName$fileName",
|
||||
};
|
||||
|
||||
var uploadURI = "$GCS_GOOGLE_BASE_URI$GCS_GOOGLE_UPLOAD_ENDPOINT";
|
||||
|
||||
var query = paramsData.entries.map((p) => '${p.key}=${p.value}').join('&');
|
||||
|
||||
var request = http.MultipartRequest(
|
||||
"POST", Uri.parse("$uploadURI$bucketName/o?$resourceName$query"));
|
||||
|
||||
request.headers.addAll(headersData);
|
||||
request.files.add(await http.MultipartFile.fromPath(fileName, file.path,
|
||||
contentType: MediaType('image', 'png')));
|
||||
|
||||
var response = await request.send();
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Failed to upload image.');
|
||||
}
|
||||
|
||||
return response.statusCode == 200;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user