我正在尝试将 pdf 文件从谷歌驱动器上传到 s3 服务器,但它没有正确上传。
我使用谷歌驱动器选择器来获取文件这是我的
这是我的谷歌驱动器选择器代码:-(客户端代码)
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
// The Browser API key obtained from the Google Developers Console.
// Replace with your own Browser API key, or your own key.
var developerKey = 'XXXXXXXXXX_ff_NX66eb-XXXXXXXXXXX';
// The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
var clientId = "XXXXXXXXXX-hs4ujaro5sc3d0g8qndtcq2tl279cfm1.apps.googleusercontent.com"
// Replace with your own App ID. (Its the first number in your Client ID)
var appId = "XXXXXXXXXX";
// Scope to use to access user's Drive items.
var scope = ['https://www.googleapis.com/auth/drive'];
var pickerApiLoaded = false;
var oauthToken;
$scope.addGoogleDriveFile = function() {
if (!isValidSelectedCategories(getSelectedCategories())) {
return;
}
if (!isValidSelectedCategoriesNoapost(getSelectedCategories())) {
return;
}
gapi.load('auth', {'callback': onAuthApiLoad});
gapi.load('picker', {'callback': onPickerApiLoad});
}
function onAuthApiLoad() {
window.gapi.auth.authorize(
{
'client_id': clientId,
'scope': scope,
'immediate': false
},
handleAuthResult);
}
function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for searching images.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var view = new google.picker.View(google.picker.ViewId.DOCS);
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.setAppId(appId)
.setOAuthToken(oauthToken)
.addView(view)
.addView(new google.picker.DocsUploadView())
.setDeveloperKey(developerKey)
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
console.log(data);
addGoogleDrivePDF (data.docs);
}
}
var addGoogleDrivePDF = function (file) {
var pdfData = {
url: file[0].url
};
$http.post('/uploadto/s3/drtivepdf', pdfData )
.success((function (article) {
console.log(article);
}).error(function (error) {
console.log(article);
});
}
在这里我可以获取文件网址
这是我的服务器端代码
s3upload_rou.js 文件(路由器文件)
app.post('/uploadto/s3/drtivepdf', function (req, res, next) {
next();
}, bookmark.addGoogleDrivePDF );
s3upload_ctrl.js 文件(控制器文件)
var async = require('async');
var config = require('../config');
var file = require('../libs/file');
exports.addDropboxBookmark = function (req, res, next) {
saveGoogleDrivePdf(req.body.url, function (err, pdfDetail) {
if (err) {
cb(err);
} else {
res.send(pdfDetail);
}
});
}
var saveGoogleDrivePdf= function (url, callback) {
async.waterfall([
function (cb) {
var fileDetails = {
url: url,
name: 'drive/' + url.toString().replace(/^(http|https):\/\//i, ""),
contentType: 'application/pdf'
};
file.uploadPdf(fileDetails);
cb(null, fileDetails);
},function (fileDetails, cb) {
console.log(fileDetails);
//create object for saving the pdf
var pdfDetail = {
pdf_url: fileDetails.url,
pdf_title: 'PDF FILE',
pdf_preview_image: config.defaultPreviewImageUrl,
pdf_file: fileDetails.name,
pdf_website: url.parse(url, false, true).host,
mode: 'pdf'
};
cb(null, pdfDetail );
}
], function (err, pdfDetail ) {
if (err) {
callback(err);
} else {
callback(null, pdfDetail );
}
});
}
这是我的 file.js 代码:-'
var request = require('request');
var config = require('../config');
var url = require('url');
var http = require('http');
var pool = new http.Agent();
var streamingS3 = require('streaming-s3');
exports.uploadPdf = function (details, cb) {
var options = {
url: config.s3.url + 'upload/drive',
method: 'POST',
json: true,
body: details,
headers : { "x-api-key": config.s3.apiKey, "x-aws-bucket": config.aws.bucket, "Content-Type": 'application/json' },
agent: pool
};
request(options, function (err, res) {
if (err) {
console.log(err);
logger.log('error', "PDF Error: ", { error : err , details: details });
} else {
console.log(res.body);
logger.log('info', "UPLOAD PDF", { response : res.body });
}
});
};
上传工作正常,但是当我尝试在 S3 上查看该文件时,我无法打开它。
任何原因 ?
谢谢