Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TQuickAzure.PutBlob Filesize Issue #32

Closed
CPsoftBE opened this issue Apr 14, 2020 · 8 comments
Closed

TQuickAzure.PutBlob Filesize Issue #32

CPsoftBE opened this issue Apr 14, 2020 · 8 comments
Labels

Comments

@CPsoftBE
Copy link

Hi,

When you upload a file to Azure using PutBlob function then the filesize is wrong on Azure. When you download this file again, tested with an excel file, it will be damaged (it will ask for a repair).
The reason is the way the local var Content is handled. I found a fix in the Embarcadero 10.3.3 Demos on GitHub, filename CloudPopulator.pas (Trunk\Object Pascal\Database\CloudAPI\CloudExplorer).

`function TQuickAzure.PutBlob(const azContainer : string; cStream : TStream; const azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
//Content : TArray;
Content: TBytes;
CloudResponseInfo : TCloudResponseInfo;
container : string;
blobname : string;
begin
azResponseInfo.StatusCode := 500;
if cStream.Size = 0 then
begin
azResponseInfo.StatusMsg := 'Stream is empty';
Exit;
end;

container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
try
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
// Content := StreamToArray(cStream);
Content := ByteContent(cStream);
Result := BlobService.PutBlockBlob(container,blobname,Content,EmptyStr,nil,nil,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
end;
except
on E : Exception do
begin
azResponseInfo.StatusCode := 500;
azResponseInfo.StatusMsg := e.message;
Result := False;
end;
end;
end;`

and added (copied from the Embarcadero Demos on GitHub, CloudPopulator.pas)

`function TQuickAzure.ByteContent(DataStream: TStream): TBytes;
var
Buffer: TBytes;
begin
if not Assigned(DataStream) then
exit(nil);

SetLength(Buffer, DataStream.Size);
// the content may have been read
DataStream.Position := 0;
if DataStream.Size > 0 then
DataStream.Read(Buffer[0], DataStream.Size);

Result := Buffer;
end;`

Regards,
Pascal

@exilon
Copy link
Owner

exilon commented Apr 22, 2020

It's strange. This function is working for many years in production systems without any issues.
I'll test it. Thanks for your suggestion.

@exilon
Copy link
Owner

exilon commented Apr 22, 2020

Have you tried the filename-based version instead? Have you obtained the same size issue?

@exilon
Copy link
Owner

exilon commented Apr 22, 2020

I've tested current code with RAD Studio 10.3.3 uploading jpg images without problems. Which delphi version are you using?

@CPsoftBE
Copy link
Author

CPsoftBE commented Apr 22, 2020 via email

@CPsoftBE
Copy link
Author

I'll try the filename-based version asap

@exilon
Copy link
Owner

exilon commented Apr 22, 2020

Try stream version with other file types different to xls, like jpg, please.

@CPsoftBE
Copy link
Author

CPsoftBE commented May 1, 2020

Tested with filename-based version: not ok (corrupted xls).

Test results:

  1. Upload using filename-based version:

Pdf: Size on Azure is 112 KiB
Pdf file can be opened without warning

Xls: Size on Azure is 280 KiB
Xls file corrupted, need to be repaired warning

  1. Upload using modified code stream-based version:

Pdf: Size on Azure is 108.3 KiB
Pdf file can be opened without warning

Xls: Size on Azure is 273.96 KiB
Xls file can be opened without warning

@exilon exilon added the bug label Jun 27, 2020
@exilon
Copy link
Owner

exilon commented Nov 5, 2020

Fixed in next version. Thanks for your report and solution.

@exilon exilon closed this as completed Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants