如何解决将Blob数据从MySql DB转换到Google云存储时出错
我正在尝试进行数据迁移。我们曾经将图像作为中等blob数据存储在MySQL DB中,我想读取此图像(blob数据)并上传到Google云存储桶。上传有效,但是图像在云存储中已损坏。
上传文件的代码
const id: string = uuidv4();
const buffer: Buffer = image.ImageBin; //image.ImageBin contains blob data from DB
let file = {
originalname: imageId + ".JPG",buffer,};
return await upload(file,id);
上传功能
public async upload(file,imageId): Promise<string> {
try {
const bucket = this.storage.bucket(process.env.IMAGE_BUCKET);
return new Promise((resolve,reject) => {
let { originalname,buffer } = file;
originalname = imageId + ":" + originalname;
originalname = originalname.replace(/ /g,"_");
const blob = bucket.file(originalname);
const blobStream = blob.createWriteStream({
resumable: false,});
blobStream
.on("finish",() => {
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
);
resolve(originalname);
})
.on("error",(error) => {
reject(error);
})
.end(buffer);
});
} catch (error) {
this.logger.log(`error uploading file error:${error}`);
}
}
以前用于从mysql获取图像的Java代码,此代码工作正常。
public Response getImage(Integer imageid,HttpServletResponse resp)
{
Response response = null;
UserPhotoUpload UserPhotoUpload = new UserPhotoUpload();
Blob imageBlob;
OutputStream out = null;
try
{
// get the image to upload
UserPhotoUpload = userPhotoUploadDAO.getImage(imageid);
if (UserPhotoUpload != null)
{
imageBlob = UserPhotoUpload.getImageBin();
if (imageBlob != null)
{
// get the output stream
out = resp.getOutputStream();
// add header info
resp.setHeader("Content-Disposition","inline;filename=test.JPG");
resp.setContentType("Content-type: image/JPG");
// copy the image to the output stream
IOUtils.copy(imageBlob.getBinaryStream(),out);
out.flush();
out.close();
}
else
throw new ScottsAppException(CommonConstants.STATUS_MSG_ERROR_IMAGE_NOT_EXISTS);
}
else
throw new ScottsAppException(CommonConstants.STATUS_MSG_ERROR_IMAGE_NOT_EXISTS);
}
catch (ScottsAppException e)
{
response = new Response();
CommonUtil.responseErrorUpdate(response,e.getMessage(),CommonConstants.STATUS_CODE_APPLICATION_FAILURE);
}
catch (IOException e)
{
response = new Response();
log.error("Error",e);
CommonUtil.responseErrorUpdate(response,CommonConstants.STATUS_MSG_MISC_APPLICATION_FAILURE,CommonConstants.STATUS_CODE_APPLICATION_FAILURE);
}
catch (SQLException e)
{
response = new Response();
log.error("Error",CommonConstants.STATUS_CODE_APPLICATION_FAILURE);
}
catch (Exception e)
{
response = new Response();
log.error("Error",CommonConstants.STATUS_CODE_APPLICATION_FAILURE);
}
finally
{
try
{
out.flush();
}
catch (Exception e)
{
}
try
{
out.close();
}
catch (Exception e)
{
}
}
// return
return response;
}
如果有人不想看blob数据https://justpaste.it/862mk
解决方法
对于来这里的人来说,问题出在从数据库复制blob数据。 上面的功能工作正常。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。