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

Cannot upload empty files - Missing input parameters #24

Closed
capsandiego opened this issue Oct 14, 2022 · 3 comments
Closed

Cannot upload empty files - Missing input parameters #24

capsandiego opened this issue Oct 14, 2022 · 3 comments
Assignees
Labels

Comments

@capsandiego
Copy link

I'm trying to use putObject method to upload small files into my S3 bucket. The non-empty files will get uploaded correctly, while the empty ones will not get uploaded. Is it possible to upload empty files?
Here is my code:

use Akeeba\Engine\Postproc\Connector\S3v4\Configuration;
use Akeeba\Engine\Postproc\Connector\S3v4\Connector;
use Akeeba\Engine\Postproc\Connector\S3v4\Input;

defined('AKEEBAENGINE') or define('AKEEBAENGINE', 1);
define('S3KEY',    '------------');
define('S3SECRET', '-----------');
define('S3REGION', 'us-east-1');
define('S3BUCKET', 'mybucket');

$configuration = new Configuration( S3KEY, S3SECRET, 'v4', S3REGION);
$connector      = new Connector($configuration);
$folder             = "uat";
$bucket            = S3BUCKET;

// ------------- THIS WORKS ----------------
$file          = "non-empty-file.csv"; // ~15bytes
$input       = Input::createFromFile($file);

try
{
    $connector->putObject($input, $bucket, $folder."/non-empty-file.csv");
}
catch(Exception $e)
{
    echo "\n Exception: ".$e->getMessage();
}


// ------------- THIS GENERATES AN [Missing input parameters] EXCEPTION ----------------
$file          = "empty-file.csv"; // 0bytes
$input       = Input::createFromFile($file);

try
{
    $connector->putObject($input, $bucket, $folder."/empty-file.csv");
}
catch(Exception $e)
{
    echo "\n Exception: ".$e->getMessage();
}

@nikosdion
Copy link
Member

You can't upload empty files. This is forbidden by the following code.

s3/src/Connector.php

Lines 82 to 88 in 6448ddf

if (($input->getSize() <= 0) || (($input->getInputType() == Input::INPUT_DATA) && (!strlen($input->getDataReference()))))
{
if (substr($uri, -1) !== '/')
{
throw new CannotPutFile('Missing input parameters', 0);
}
}

The empty object upload is only allowed if the last character is the path separator (/) as this is the "cheat" many third party applications managing S3 buckets, e.g. CyberDuck, use to create new and empty "folders".

In your use case you could probably detect an empty CSV file and upload a single newline ("\n") using $empty = "\n"; $input = Input::createFromData($empty);.

@nikosdion nikosdion self-assigned this Oct 14, 2022
@capsandiego
Copy link
Author

Thank you, the "cheat" works well.
Just out of curiosity, why is there this limitation? I see other tools (for example My S3 Browser) which can upload 0 bytes files. With your trick I get at least 1 byte file in the S3 folder.

@nikosdion
Copy link
Member

I have added this limitation because there was a very common issue where people would forget to initialise the Input object correctly. At the same time uploading zero-length items is an exceedingly rare case. Catching the zero-length case and throwing an exception (which can easily trigger a debug trace if you use any kind of exception handling or even PHP's bog standard error logging) was much more productive than having people trying to hunt down the Input initialisation problem.

I am going to label this as “won't fix” since throwing the exception is still more relevant than allowing zero-length uploads. If this changes in the future I will of course revise the code.

@nikosdion nikosdion closed this as not planned Won't fix, can't repro, duplicate, stale Oct 14, 2022
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