Date: 2024-10-19 09:00:04
Score: 0.5
Natty:
load->library('S3Uploader');
$this->load->model('User_model');
$this->sqsClient = new Aws\Sqs\SqsClient([
'version' => 'latest',
'region' => 'your-region',
'credentials' => [
'key' => 'your-key',
'secret' => 'your-secret',
],
]);
$this->queueUrl = 'https://sqs.your-region.amazonaws.com/your-account-id/your-queue-name';
}
public function process_queue()
{
while (true) {
$result = $this->sqsClient->receiveMessage([
'QueueUrl' => $this->queueUrl,
'MaxNumberOfMessages' => 10, // Adjust as needed
'WaitTimeSeconds' => 20, // Long polling
]);
if (!isset($result['Messages'])) {
continue;
}
foreach ($result['Messages'] as $message) {
$body = json_decode($message['Body'], true);
$userId = $body['user_id'];
$localFilePath = $body['local_path'];
$s3Key = $body['s3_key'];
$uploadSuccess = $this->s3uploader->uploadFile($localFilePath, $s3Key);
if ($uploadSuccess) {
$s3Path = 's3://your-bucket/' . $s3Key;
$this->User_model->update_user_log_path($userId, $s3Path);
// Delete the message from the queue
$this->sqsClient->deleteMessage([
'QueueUrl' => $this->queueUrl,
'ReceiptHandle' => $message['ReceiptHandle'],
]);
// Delete local file if needed
unlink($localFilePath);
} else {
// Handle failure and optionally retry
}
}
}
}
}
Reasons:
- Long answer (-1):
- No code block (0.5):
- Low reputation (1):
Posted by: user27882047